Skip to content

Commit

Permalink
Merge pull request #294 from jaredwray/upgrading-to-vitest-and-pnpm
Browse files Browse the repository at this point in the history
Upgrading to vitest and pnpm
  • Loading branch information
jaredwray committed Apr 14, 2024
2 parents d1f3cb3 + fa65498 commit 7fcbecf
Show file tree
Hide file tree
Showing 21 changed files with 10,492 additions and 65 deletions.
28 changes: 16 additions & 12 deletions .github/workflows/tests.yaml
Expand Up @@ -14,22 +14,26 @@ jobs:
node: ['18', '20']
name: Node ${{ matrix.node }}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Setup node
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}

- name: Install Dependencies
run: yarn

- name: Run Build
run: yarn build

- name: Test Packages
run: yarn test
- name: Install PNPM
run: npm install -g pnpm

- name: Code Coverage
uses: codecov/codecov-action@v2
- name: Install dependencies
run: pnpm install

- name: Build
run: pnpm build

- name: Test
run: pnpm test:ci

- name: Codecov
uses: codecov/codecov-action@v3
with:
files: ./coverage/lcov.info
token: ${{ secrets.CODECOV_KEY }}
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -9,7 +9,7 @@

# How to Use the Cacheable Mono Repo

Cacheable has the main package `cacheable-request` under `/packages/request` and its website. In addtion we have a couple of other documents for review:
Cacheable has the main package `cacheable-request` under `/packages/cacheable-request` and its website. In addtion we have a couple of other documents for review:

* [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) - Our code of conduct
* [CONTRIBUTING.md](CONTRIBUTING.md) - How to contribute to this project
Expand All @@ -21,7 +21,7 @@ You can contribute changes to this repo by opening a pull request:

1) After forking this repository to your Git account, make the proposed changes on your forked branch.
2) Run tests and linting locally.
- Run `yarn && yarn test`.
- Run `pnpm i && pnpm test`.
3) Commit your changes and push them to your forked repository.
4) Navigate to the main `cacheable` repository and select the *Pull Requests* tab.
5) Click the *New pull request* button, then select the option "Compare across forks"
Expand Down
13 changes: 7 additions & 6 deletions package.json
Expand Up @@ -10,18 +10,19 @@
"packages/*"
],
"scripts": {
"build": "yarn workspaces run build",
"test": " yarn build && yarn workspaces run test",
"website:build": "yarn workspace @cacheable/website run build",
"clean": "rm -rf node_modules && rm -rf yarn.lock && yarn workspaces run clean"
"build": "pnpm recursive run build",
"test": " pnpm build && pnpm recursive run test",
"test:ci": "c8 --reporter=lcov pnpm recursive run test:ci",
"website:build": "pnpm run build --filter @cacheable/website",
"clean": "rimraf node_modules yarn.lock pnpm-lock.yaml && pnpm run clean --recursive"
},
"dependencies": {
"@types/node": "^20.11.5",
"webpack": "^5.89.0"
},
"devDependencies": {
"eslint-plugin-jest": "^27.6.3",
"jest": "^29.7.0",
"c8": "^9.1.0",
"rimraf": "^5.0.5",
"ts-node": "^10.9.2",
"typescript": "^5.3.3",
"xo": "^0.56.0"
Expand Down
File renamed without changes.
File renamed without changes.
Expand Up @@ -12,10 +12,11 @@
"node": ">=18"
},
"scripts": {
"test": "xo --fix && NODE_OPTIONS=--experimental-vm-modules jest --coverage ",
"prepare": "npm run build",
"test": "xo --fix && vitest run --coverage",
"test:ci": "xo && vitest run",
"prepare": "pnpm run build",
"build": "tsc --project tsconfig.build.json",
"clean": "rm -rf node_modules && rm -rf ./coverage && rm -rf ./test/testdb.sqlite && rm -rf ./dist"
"clean": "rimraf node_modules ./coverage ./test/testdb.sqlite ./dist"
},
"files": [
"dist"
Expand Down Expand Up @@ -50,18 +51,21 @@
"@types/node": "^20.11.30",
"@types/responselike": "^1.0.3",
"@types/sqlite3": "^3.1.11",
"@vitest/coverage-v8": "^1.5.0",
"body-parser": "^1.20.2",
"delay": "^6.0.0",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^27.9.0",
"express": "^4.19.1",
"jest": "^29.7.0",
"pify": "^6.1.0",
"rimraf": "^5.0.5",
"sqlite3": "^5.1.7",
"ts-jest": "^29.1.2",
"ts-jest-resolver": "^2.0.1",
"ts-node": "^10.9.2",
"typescript": "^5.4.3",
"vitest": "^1.5.0",
"xo": "^0.58.0"
},
"jest": {
Expand Down
File renamed without changes.
File renamed without changes.
Expand Up @@ -2,6 +2,9 @@ import {Agent, request} from 'node:http';
import url from 'node:url';
import util, {promisify as pm} from 'node:util';
import {gzip, gunzip} from 'node:zlib';
import {
test, beforeAll, afterAll, expect,
} from 'vitest';
import getStream from 'get-stream';
import delay from 'delay';
import sqlite3 from 'sqlite3';
Expand Down Expand Up @@ -230,26 +233,25 @@ const testCacheKey = async (input: any, expected: string) => {
await expect(cacheableRequestHelper(input)).rejects.toThrow();
};

// eslint-disable-next-line jest/expect-expect
test('return with GET', async () => testCacheKey('http://www.example.com', 'GET:http://www.example.com'));
// eslint-disable-next-line jest/expect-expect

test(
'strips default path',
async () => testCacheKey('http://www.example.com/', 'GET:http://www.example.com'),
);
// eslint-disable-next-line jest/expect-expect

test(
'keeps trailing /',
async () => testCacheKey('http://www.example.com/test/', 'GET:http://www.example.com/test/'),
);
// eslint-disable-next-line jest/expect-expect

test(
'return with GET.',
async () => testCacheKey(new url.URL('http://www.example.com'), 'GET:http://www.example.com'),
);
// eslint-disable-next-line jest/expect-expect

test('no requried properties', async () => testCacheKey({}, 'GET:http://localhost'));
// eslint-disable-next-line jest/expect-expect

test(
'return without slash',
async () => testCacheKey(
Expand All @@ -261,7 +263,7 @@ test(
},
'GET:http://www.example.com',
));
// eslint-disable-next-line jest/expect-expect

test(
'return without port',
async () => testCacheKey(
Expand All @@ -272,7 +274,7 @@ test(
},
'GET:http://www.example.com',
));
// eslint-disable-next-line jest/expect-expect

test(
'return with url and port',
async () => testCacheKey(
Expand All @@ -283,9 +285,9 @@ test(
},
'GET:http://www.example.com:8080',
));
// eslint-disable-next-line jest/expect-expect

test('return with protocol', async () => testCacheKey({host: 'www.example.com'}, 'GET:http://www.example.com'));
// eslint-disable-next-line jest/expect-expect

test(
'hostname over host',
async () => testCacheKey(
Expand All @@ -295,14 +297,14 @@ test(
},
'GET:http://xyz.example.com',
));
// eslint-disable-next-line jest/expect-expect

test(
'hostname defaults to localhost',
async () => testCacheKey(
{path: '/'},
'GET:http://localhost',
));
// eslint-disable-next-line jest/expect-expect

test(
'ignores pathname',
async () => testCacheKey(
Expand All @@ -312,7 +314,7 @@ test(
},
'GET:http://localhost/foo',
));
// eslint-disable-next-line jest/expect-expect

test(
'ignores search',
async () => testCacheKey(
Expand All @@ -322,7 +324,7 @@ test(
},
'GET:http://localhost/?foo=bar',
));
// eslint-disable-next-line jest/expect-expect

test(
'ignores query',
async () => testCacheKey(
Expand All @@ -332,9 +334,9 @@ test(
},
'GET:http://localhost/?foo=bar',
));
// eslint-disable-next-line jest/expect-expect

test('auth should be in url', async () => testCacheKey({auth: 'user:pass'}, 'GET:http://user:pass@localhost'));
// eslint-disable-next-line jest/expect-expect

test('should return default url', async () => testCacheKey({method: 'POST'}, 'POST:http://localhost'));
test('request options path query is passed through', async () => {
const cacheableRequest = new CacheableRequest(request);
Expand Down
@@ -1,7 +1,7 @@
import {request} from 'node:http';
import {test, expect} from 'vitest';
import Keyv from 'keyv';
import CacheableRequest from '../src/index.js';
import {CacheError} from '../src/types.js';

test('CacheableRequest is a function', () => {
expect(typeof CacheableRequest).toBe('function');
Expand Down
Expand Up @@ -2,6 +2,9 @@ import EventEmitter from 'node:events';
import {request} from 'node:http';
import stream from 'node:stream';
import url from 'node:url';
import {
test, expect, beforeAll, afterAll,
} from 'vitest';
import getStream from 'get-stream';
import CacheableRequest from '../src/index.js';
import {CacheError, RequestError} from '../src/types.js';
Expand Down Expand Up @@ -83,16 +86,14 @@ test('cacheableRequest emits response event for cached responses', () => {
});
}).on('request', (request_: any) => request_.end());
});
test('cacheableRequest emits CacheError if cache adapter connection errors', done => { // eslint-disable-line jest/no-done-callback
test('cacheableRequest emits CacheError if cache adapter connection errors', () => {
const cacheableRequest = new CacheableRequest(request, 'sqlite://non/existent/database.sqlite').request();
cacheableRequest(url.parse(s.url))
.on('error', (error: any) => {
expect(error instanceof CacheError).toBeTruthy();
if (error.code === 'SQLITE_CANTOPEN') {
expect(error.code).toBe('SQLITE_CANTOPEN'); // eslint-disable-line jest/no-conditional-expect
expect(error.code).toBe('SQLITE_CANTOPEN');
}

done();
})
.on('request', (request_: any) => request_.end());
});
Expand Down Expand Up @@ -134,7 +135,7 @@ test('cacheableRequest emits CacheError if cache.set errors', () => {
})
.on('request', (request_: any) => request_.end());
});
test('cacheableRequest emits CacheError if cache.delete errors', done => { // eslint-disable-line jest/no-done-callback
test('cacheableRequest emits CacheError if cache.delete errors', () => {
const errorMessage = 'Fail';
const store = new Map();
const cache = {
Expand Down Expand Up @@ -164,25 +165,23 @@ test('cacheableRequest emits CacheError if cache.delete errors', done => { // es
expect(error instanceof CacheError).toBeTruthy();
expect(error.message).toBe(errorMessage);
await s.close();
done();
})
.on('request', (request_: any) => request_.end());
});
}).on('request', (request_: any) => request_.end());
})();
});
test('cacheableRequest emits RequestError if request function throws', done => { // eslint-disable-line jest/no-done-callback
test('cacheableRequest emits RequestError if request function throws', () => {
const cacheableRequest = new CacheableRequest(request).request();
const options: any = url.parse(s.url);
options.headers = {invalid: '💣'};
cacheableRequest(options)
.on('error', (error: any) => {
expect(error instanceof RequestError).toBeTruthy();
done();
})
.on('request', (request_: any) => request_.end());
});
test('cacheableRequest does not cache response if request is aborted before receiving first byte of response', done => { // eslint-disable-line jest/no-done-callback
test('cacheableRequest does not cache response if request is aborted before receiving first byte of response', () => {
/* eslint-disable max-nested-callbacks */

void createTestServer().then(s => {
Expand All @@ -207,14 +206,13 @@ test('cacheableRequest does not cache response if request is aborted before rece
const body = await getStream(response);
expect(body).toBe('hi');
await s.close();
done();
}).on('request', (request_: any) => request_.end());
}, 100);
});
});
/* eslint-enable max-nested-callbacks */
});
test('cacheableRequest does not cache response if request is aborted after receiving part of the response', done => { // eslint-disable-line jest/no-done-callback
test('cacheableRequest does not cache response if request is aborted after receiving part of the response', () => {
/* eslint-disable max-nested-callbacks */

void createTestServer().then(s => {
Expand All @@ -240,7 +238,6 @@ test('cacheableRequest does not cache response if request is aborted after recei
const body = await getStream(response);
expect(body).toBe('hi');
await s.close();
done();
}).on('request', (request_: any) => request_.end());
}, 100);
});
Expand Down Expand Up @@ -308,7 +305,7 @@ test('cacheableRequest hashes request body as cache key', async () => {
.on('error', () => {/* do nothing */})
.on('request', (request_: any) => request_.end());
});
test('cacheableRequest skips cache for streamed body', done => { // eslint-disable-line jest/no-done-callback
test('cacheableRequest skips cache for streamed body', () => {
const cache = {
get() {
fail(new CacheError(new Error('Cache error'))); // eslint-disable-line jest/no-jasmine-globals
Expand All @@ -329,7 +326,6 @@ test('cacheableRequest skips cache for streamed body', done => { // eslint-disab
options.method = 'POST';
cacheableRequest(options, (response_: any) => {
expect(response_.statusCode).toBe(201);
done();
})
.on('error', () => {/* do nothing */})
.on('request', (request_: any) => request_.end());
Expand All @@ -346,14 +342,13 @@ test('cacheableRequest makes request and cancelled)', async () => {
.on('request', (request_: any) => request_.abort());
});

test('cacheableRequest emits CacheError if request cancels', done => { // eslint-disable-line jest/no-done-callback
test('cacheableRequest emits CacheError if request cancels', () => {
const cacheableRequest = new CacheableRequest(request).request();
const options: any = url.parse(s.url);
options.headers = {invalid: '💣'};
cacheableRequest(options)
.on('error', (error: any) => {
expect(error instanceof CacheError).toBeTruthy();
done();
})
.on('request', (request_: any) => request_.abort());
});
@@ -1,4 +1,5 @@
import {readFileSync} from 'node:fs';
import {test, expect} from 'vitest';

test('@types/http-cache-semantics is a regular (not dev) dependency', () => {
// Required to avoid `Could not find a declaration file for module 'http-cache-semantics'` error from `tsc` when using this package in other projects
Expand Down
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions packages/cacheable-request/vitest.config.mjs
@@ -0,0 +1,14 @@
import {defineConfig} from 'vitest/config';

export default defineConfig({
test: {
coverage: {
exclude: [
'site/docula.config.cjs',
'site-output/**',
'.pnp.*',
'.yarn/**',
],
},
},
});

0 comments on commit 7fcbecf

Please sign in to comment.