Tests/shared (#14836)

* Move from jest to vitest

* Move test files, cleanup imports
This commit is contained in:
Rijk van Zanten
2022-08-03 17:26:17 -04:00
committed by GitHub
parent 0f64c76d0d
commit 855db5638f
31 changed files with 154 additions and 300 deletions

View File

@@ -40,14 +40,14 @@
"build:cjs": "tsc --project ./tsconfig.json --module CommonJS --outDir ./dist/cjs",
"cleanup": "rimraf ./dist",
"dev": "pnpm build -- -w --preserveWatchOutput --incremental",
"test": "jest",
"test:watch": "jest --watchAll"
"test": "vitest run",
"test:coverage": "vitest coverage",
"test:watch": "vitest"
},
"author": "Nicola Krumschmidt",
"maintainers": [
"Rijk van Zanten <rijkvanzanten@me.com>"
],
"gitHead": "24621f3934dc77eb23441331040ed13c676ceffd",
"dependencies": {
"axios": "^0.27.2",
"date-fns": "2.24.0",
@@ -65,19 +65,16 @@
"vue-router": "4.0.15"
},
"devDependencies": {
"@jest/types": "^28.1.3",
"@types/express": "4.17.13",
"@types/fs-extra": "9.0.13",
"@types/geojson": "7946.0.8",
"@types/jest": "^28.1.4",
"@types/lodash": "^4.14.182",
"@types/pino": "6.3.12",
"@types/tmp": "^0.2.3",
"jest": "^28.1.2",
"npm-run-all": "4.1.5",
"rimraf": "3.0.2",
"tmp": "0.2.1",
"ts-jest": "^28.0.5",
"typescript": "4.5.2"
"typescript": "4.5.2",
"vitest": "^0.18.1"
}
}

View File

@@ -1,4 +1,5 @@
import { abbreviateNumber } from '../../src/utils/abbreviate-number';
import { describe, expect, it } from 'vitest';
import { abbreviateNumber } from './abbreviate-number';
describe('when no unit is given', () => {
it('when under 1000', () => {

View File

@@ -1,5 +1,6 @@
import { RawField } from '../../src/types/fields';
import { addFieldFlag } from '../../src/utils/add-field-flag';
import { describe, expect, it } from 'vitest';
import { RawField } from '../types/fields';
import { addFieldFlag } from './add-field-flag';
describe('addFieldFlag', () => {
it('Adds a flag to a field without meta', () => {

View File

@@ -1,4 +1,5 @@
import { adjustDate } from '../../src/utils/adjust-date';
import { describe, expect, it } from 'vitest';
import { adjustDate } from './adjust-date';
describe('Adjust a given date by a given change in duration.', () => {
const date = new Date('2021-09-20T21:06:51.517Z');

View File

@@ -1,4 +1,5 @@
import { isIn, isTypeIn } from '../../src/utils/array-helpers';
import { describe, expect, it } from 'vitest';
import { isIn, isTypeIn } from './array-helpers';
describe('type helpers for arrays', () => {
const array = ['foo', 'bar'] as const;

View File

@@ -1,4 +1,5 @@
import { deepMap } from '../../src/utils/deep-map';
import { describe, expect, it } from 'vitest';
import { deepMap } from './deep-map';
describe('deepMap', () => {
const mockIterator = (val: any, _key: string | number) => {

View File

@@ -1,16 +1,17 @@
import {
defineInterface,
defineDisplay,
defineModule,
defineLayout,
defineHook,
defineEndpoint,
defineOperationApp,
defineOperationApi,
definePanel,
} from '../../src/utils/define-extension';
import { Type } from '../../src/types/fields';
import { describe, expect, it } from 'vitest';
import { defineComponent } from 'vue';
import { Type } from '../types/fields';
import {
defineDisplay,
defineEndpoint,
defineHook,
defineInterface,
defineLayout,
defineModule,
defineOperationApi,
defineOperationApp,
definePanel,
} from './define-extension';
const mockComponent = defineComponent({});
const mockHandler = () => {

View File

@@ -1,4 +1,5 @@
import { functions } from '../../src/utils/functions';
import { describe, expect, it } from 'vitest';
import { functions } from './functions';
describe('Data Functions', () => {
describe('year', () => {

View File

@@ -1,6 +1,7 @@
import { escapeRegExp } from 'lodash';
import { FieldFilter } from '../../src/types';
import { generateJoi, Joi, JoiOptions, StringSchema } from '../../src/utils/generate-joi';
import { describe, expect, it } from 'vitest';
import { FieldFilter } from '../types';
import { generateJoi, Joi, JoiOptions, StringSchema } from './generate-joi';
describe(`generateJoi`, () => {
const date = new Date(1632431505992);

View File

@@ -1,5 +1,6 @@
import { getCollectionType } from '../../src/utils';
import { Collection } from '../../src/types';
import { describe, expect, it } from 'vitest';
import { Collection } from '../types';
import { getCollectionType } from './get-collection-type';
const TableCollection: Collection = {
collection: 'table',

View File

@@ -1,4 +1,5 @@
import { getEndpoint } from '../../src/utils/get-endpoint';
import { describe, expect, it } from 'vitest';
import { getEndpoint } from './get-endpoint';
describe('getEndpoint', () => {
it('When a system collection is passed in', () => {

View File

@@ -1,4 +1,5 @@
import { getFieldsFromTemplate } from '../../src/utils/get-fields-from-template';
import { describe, expect, it } from 'vitest';
import { getFieldsFromTemplate } from './get-fields-from-template';
describe('getFieldsFromTemplate', () => {
it('returns an empty array when passed null', () => {

View File

@@ -1,4 +1,5 @@
import { getFilterOperatorsForType } from '../../src/utils/get-filter-operators-for-type';
import { describe, expect, it } from 'vitest';
import { getFilterOperatorsForType } from './get-filter-operators-for-type';
describe('', () => {
it('returns the filter operators for alias', () => {

View File

@@ -1,5 +1,6 @@
import { getRelationType } from '../../src/utils/get-relation-type';
import { Relation } from '../../src/types';
import { describe, expect, it } from 'vitest';
import { Relation } from '../types';
import { getRelationType } from './get-relation-type';
describe('getRelationType', () => {
it('returns m2o when relation is the same as collection and field', () => {

View File

@@ -1,4 +1,5 @@
import { getSimpleHash } from '../../src/utils/get-simple-hash';
import { describe, expect, it } from 'vitest';
import { getSimpleHash } from './get-simple-hash';
describe('getSimpleHash', () => {
it('returns "364492" for string "test"', () => {

View File

@@ -1,4 +1,5 @@
import { injectFunctionResults } from '../../src/utils/inject-function-results';
import { describe, expect, it } from 'vitest';
import { injectFunctionResults } from './inject-function-results';
describe('injectFunctionResults', () => {
it('Passes the original object unchanged if no filter rules apply', () => {

View File

@@ -1,4 +1,5 @@
import { isDynamicVariable } from '../../src/utils/is-dynamic-variable';
import { describe, expect, it } from 'vitest';
import { isDynamicVariable } from './is-dynamic-variable';
const tests: [string, boolean][] = [
['$NOW', true],

View File

@@ -1,4 +1,5 @@
import { mergeFilters } from '../../src/utils/merge-filters';
import { describe, expect, it } from 'vitest';
import { mergeFilters } from './merge-filters';
describe('merge filters', () => {
it('defaults to A when B is null', () => {

View File

@@ -1,4 +1,5 @@
import { moveInArray } from '../../src/utils/move-in-array';
import { describe, expect, it } from 'vitest';
import { moveInArray } from './move-in-array';
describe('moveInArray', () => {
const testArray = [1, 2, 3, 4, 5, 6];

View File

@@ -1,6 +1,8 @@
import { ensureExtensionDirs } from '../../../src/utils/node/ensure-extension-dirs';
import { EXTENSION_TYPES } from '../../../src/constants/extensions';
import { dirSync, DirResult } from 'tmp';
import { describe, beforeEach, afterEach, it, expect } from 'vitest';
import { DirResult, dirSync } from 'tmp';
import { EXTENSION_TYPES } from '../../constants/extensions';
import { ensureExtensionDirs } from './ensure-extension-dirs';
describe('ensureExtensionDirs', () => {
let rootDir: DirResult;

View File

@@ -1,5 +1,7 @@
import { generateExtensionsEntry } from '../../../src/utils/node/generate-extensions-entry';
import { Extension } from '../../../src/types/extensions';
import { describe, expect, it } from 'vitest';
import { Extension } from '../../types/extensions';
import { generateExtensionsEntry } from './generate-extensions-entry';
describe('generateExtensionsEntry', () => {
const type = 'panel';

View File

@@ -1,5 +1,6 @@
import { listFolders } from '../../../src/utils/node/list-folders';
import { dirSync, DirResult } from 'tmp';
import { DirResult, dirSync } from 'tmp';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { listFolders } from './list-folders';
describe('', () => {
let rootDir: DirResult;

View File

@@ -1,15 +1,17 @@
import { parseFilter } from '../../src/utils/parse-filter';
import { vi, afterEach, beforeEach, describe, expect, it } from 'vitest';
import { Filter } from '../../src/types/filter';
import { parseFilter } from './parse-filter';
describe('', () => {
beforeEach(() => {
jest.useFakeTimers();
jest.setSystemTime(new Date(1632431505992));
vi.useFakeTimers();
vi.setSystemTime(new Date(1632431505992));
});
afterEach(() => {
jest.useRealTimers();
vi.useRealTimers();
});
it('returns the filter when passed accountability with only a role', () => {
const mockFilter = { _and: [{ field: { _eq: 'field' } }] } as Filter;
const mockAccountability = { role: 'admin' };

View File

@@ -1,4 +1,5 @@
import { noproto, parseJSON } from '../../src/utils/parse-json';
import { describe, expect, it } from 'vitest';
import { noproto, parseJSON } from './parse-json';
describe('noproto', () => {
it('Returns the value if the key is not __proto__', () => {

View File

@@ -1,4 +1,5 @@
import { pluralize, depluralize } from '../../src/utils/pluralize';
import { describe, expect, it } from 'vitest';
import { depluralize, pluralize } from './pluralize';
describe('pluralize', () => {
it('adds an s to the end of the string', () => {

View File

@@ -1,4 +1,5 @@
import { toArray } from '../../src/utils/to-array';
import { describe, expect, it } from 'vitest';
import { toArray } from './to-array';
describe('toArray', () => {
it('takes in a string and returns an array', () => {

View File

@@ -1,4 +1,5 @@
import { validateExtensionManifest } from '../../src/utils/validate-extension-manifest';
import { describe, expect, it } from 'vitest';
import { validateExtensionManifest } from './validate-extension-manifest';
describe('', () => {
it('returns false when passed item has no name or version', () => {

View File

@@ -1,5 +1,6 @@
import { validatePayload } from '../../src/utils/validate-payload';
import { describe, expect, it } from 'vitest';
import { Filter } from '../../src/types/filter';
import { validatePayload } from './validate-payload';
describe('validatePayload', () => {
it('returns an empty array when there are no errors', () => {

View File

@@ -1,150 +0,0 @@
import { EXTENSION_PACKAGE_TYPES, EXTENSION_TYPES, HYBRID_EXTENSION_TYPES } from '../../../src/constants/extensions';
import { getLocalExtensions, getPackageExtensions } from '../../../src/utils/node/get-extensions';
import { DirResult, dirSync } from 'tmp';
import { ensureDirSync, writeJsonSync } from 'fs-extra';
describe('getPackageExtensions', () => {
let rootPackageDir: DirResult;
let noPackageDir: DirResult;
beforeEach(() => {
noPackageDir = dirSync({ prefix: './' } as any);
rootPackageDir = dirSync({ unsafeCleanup: true, tmpdir: './' } as any);
});
afterEach(() => {
noPackageDir.removeCallback();
rootPackageDir.removeCallback();
});
it('throws an error when no package.json is found', async () => {
const error = async () => await getPackageExtensions(noPackageDir.name, EXTENSION_PACKAGE_TYPES);
expect(error).rejects.toThrow(`Current folder does not contain a package.json file`);
});
it('returns an array of extensions based on package.json', async () => {
const childPackage = dirSync({ tmpdir: rootPackageDir.name } as any);
writeJsonSync(`${childPackage.name}/package.json`, {
name: `${childPackage.name}`,
dependencies: {
'directus-extension-test': '0.1',
},
});
ensureDirSync(`${childPackage.name}/directus-extension-test/`);
writeJsonSync(`${childPackage.name}/directus-extension-test/package.json`, {
name: 'test',
version: '0.1',
dependencies: {},
'directus:extension': { type: 'pack', path: './', source: 'test', host: '^9.0.0' },
});
expect(await getPackageExtensions(childPackage.name, EXTENSION_PACKAGE_TYPES)).toStrictEqual([
{
children: [],
host: '^9.0.0',
local: false,
name: 'directus-extension-test',
path: childPackage.name + '/directus-extension-test',
type: 'pack',
version: '0.1',
},
]);
});
it('returns an error when validateExtensionManifest fails', async () => {
const errorPackage = dirSync({ unsafeCleanup: true, tmpdir: rootPackageDir.name } as any);
expect(getPackageExtensions(errorPackage.name, EXTENSION_PACKAGE_TYPES)).rejects.toThrowError(
`Current folder does not contain a package.json file`
);
});
it('returns an error when validateExtensionManifest fails', async () => {
const typePackage = dirSync({ unsafeCleanup: true, tmpdir: rootPackageDir.name } as any);
writeJsonSync(`${typePackage.name}/package.json`, {
name: `${typePackage.name}`,
dependencies: {
'directus-extension-type': '0.1',
},
});
ensureDirSync(`${typePackage.name}/node_modules/directus-extension-type/`);
writeJsonSync(`${typePackage.name}/node_modules/directus-extension-type/package.json`, {
name: 'test',
version: '0.1',
dependencies: {},
'directus:extension': { type: 'interface', path: './', source: 'test', host: '^9.0.0' },
});
expect(await getPackageExtensions(typePackage.name, EXTENSION_PACKAGE_TYPES)).toStrictEqual([
{
host: '^9.0.0',
entrypoint: './',
local: false,
name: 'directus-extension-type',
path: typePackage.name + '/node_modules/directus-extension-type',
type: 'interface',
version: '0.1',
},
]);
});
});
describe('getLocalExtensions', () => {
let rootLocalPackage: DirResult;
beforeEach(() => {
rootLocalPackage = dirSync({ unsafeCleanup: true, tmpdir: './' } as any);
});
afterEach(() => {
rootLocalPackage.removeCallback();
});
it(`throws an error when the extension folder can not be opened`, async () => {
expect(async () => {
await getLocalExtensions(rootLocalPackage.name, EXTENSION_TYPES);
}).rejects.toThrowError(`Extension folder "${rootLocalPackage.name}/interfaces" couldn't be opened`);
});
it(`returns an array of local extensions`, async () => {
const extensionPackages: any = [];
writeJsonSync(`${rootLocalPackage.name}/package.json`, {
name: 'test',
version: '0.1',
dependencies: {},
});
EXTENSION_TYPES.forEach((type) => {
ensureDirSync(`${rootLocalPackage.name}/${type}s/`);
writeJsonSync(`${rootLocalPackage.name}/${type}s/package.json`, {
name: `${rootLocalPackage.name}-${type}`,
dependencies: {
'directus-extension-test': '0.1',
},
});
ensureDirSync(`${rootLocalPackage.name}/${type}s/directus-extension-test`);
if (HYBRID_EXTENSION_TYPES.includes(type as any)) {
extensionPackages.push({
entrypoint: {
api: 'api.js',
app: 'app.js',
},
local: true,
name: 'directus-extension-test',
path: `${rootLocalPackage.name}/${type}s/directus-extension-test`,
type: type,
});
} else {
extensionPackages.push({
entrypoint: 'index.js',
local: true,
name: 'directus-extension-test',
path: `${rootLocalPackage.name}/${type}s/directus-extension-test`,
type: type,
});
}
});
expect(await getLocalExtensions(rootLocalPackage.name, EXTENSION_TYPES)).toStrictEqual(extensionPackages);
});
});

View File

@@ -1,23 +0,0 @@
import { resolvePackage } from '../../../src/utils/node/resolve-package';
import { dirSync, DirResult } from 'tmp';
import { ensureDirSync, writeJsonSync } from 'fs-extra';
import path from 'path';
describe('', () => {
let rootDir: DirResult;
beforeEach(() => {
rootDir = dirSync({ unsafeCleanup: true, tmpdir: './' } as any);
ensureDirSync(`${rootDir.name}/node_modules/`);
ensureDirSync(`${rootDir.name}/node_modules/test-package/`);
writeJsonSync(`${rootDir.name}/node_modules/test-package/package.json`, { name: 'test' });
});
afterEach(() => {
rootDir.removeCallback();
});
it('the package to be found', () => {
expect(resolvePackage('test-package', rootDir.name)).toBe(
path.resolve(`${rootDir.name}/node_modules/test-package`)
);
});
});

140
pnpm-lock.yaml generated
View File

@@ -767,11 +767,9 @@ importers:
packages/shared:
specifiers:
'@jest/types': ^28.1.3
'@types/express': 4.17.13
'@types/fs-extra': 9.0.13
'@types/geojson': 7946.0.8
'@types/jest': ^28.1.4
'@types/lodash': ^4.14.182
'@types/pino': 6.3.12
'@types/tmp': ^0.2.3
@@ -780,7 +778,6 @@ importers:
express: ^4.18.1
fs-extra: 10.0.0
geojson: ^0.5.0
jest: ^28.1.2
joi: 17.4.2
knex: 2.2.0
knex-schema-inspector: ^2.0.1
@@ -790,8 +787,8 @@ importers:
pino: 6.13.3
rimraf: 3.0.2
tmp: 0.2.1
ts-jest: ^28.0.5
typescript: 4.5.2
vitest: ^0.18.1
vue: 3.2.36
vue-i18n: 9.1.10
vue-router: 4.0.15
@@ -811,20 +808,17 @@ importers:
vue-i18n: 9.1.10_vue@3.2.36
vue-router: 4.0.15_vue@3.2.36
devDependencies:
'@jest/types': 28.1.3
'@types/express': 4.17.13
'@types/fs-extra': 9.0.13
'@types/geojson': 7946.0.8
'@types/jest': 28.1.5
'@types/lodash': 4.14.182
'@types/pino': 6.3.12
'@types/tmp': 0.2.3
jest: 28.1.3
npm-run-all: 4.1.5
rimraf: 3.0.2
tmp: 0.2.1
ts-jest: 28.0.6_jjaz33tpueyjzm33bmtpjj2m4i
typescript: 4.5.2
vitest: 0.18.1
packages/specs:
specifiers:
@@ -3918,14 +3912,6 @@ packages:
pretty-format: 27.5.1
dev: true
/@types/jest/28.1.5:
resolution:
{ integrity: sha512-TLAC2zXxGnohSP3GxgIyJn7yrTeRPDEyVFyCY1NE2wzg392auI+69uk5EPGjUXuhkq/K208J/TWpLG7J8ebIEQ== }
dependencies:
jest-matcher-utils: 28.1.3
pretty-format: 28.1.3
dev: true
/@types/js-yaml/4.0.5:
resolution:
{ integrity: sha512-FhpRzf927MNQdRZP0J5DLIdTXhjLYzeUTmLAu69mnVksLH9CJY3IuSeEgbKUki7GQZm0WqDkGzyxju2EZGD2wA== }
@@ -10238,27 +10224,6 @@ packages:
- ts-node
dev: true
/jest/28.1.3:
resolution:
{ integrity: sha512-N4GT5on8UkZgH0O5LUavMRV1EDEhNTL0KEfRmDIeZHSV7p2XgLoY9t9VDUgL6o+yfdgYHVxuz81G8oB9VG5uyA== }
engines: { node: ^12.13.0 || ^14.15.0 || ^16.10.0 || >=17.0.0 }
hasBin: true
peerDependencies:
node-notifier: ^8.0.1 || ^9.0.0 || ^10.0.0
peerDependenciesMeta:
node-notifier:
optional: true
dependencies:
'@jest/core': 28.1.3
'@jest/types': 28.1.3
import-local: 3.1.0
jest-cli: 28.1.3
transitivePeerDependencies:
- '@types/node'
- supports-color
- ts-node
dev: true
/jmespath/0.16.0:
resolution:
{ integrity: sha512-9FzQjJ7MATs1tSpnco1K6ayiYE3figslrXA72G2HQ/n76RzvYlofyi5QM+iX4YRs/pu3yzxlVQSST23+dMDknw== }
@@ -15071,39 +15036,6 @@ packages:
yargs-parser: 21.0.1
dev: true
/ts-jest/28.0.6_jjaz33tpueyjzm33bmtpjj2m4i:
resolution:
{ integrity: sha512-yLAWoaSJ6c9o+IT7+nyutp5uvwGzhMYb/LD5WEQAi2tBq4ZSAPay4Lf69pP/IU+GFYg87pdg5eADSzuNAFSK4g== }
engines: { node: ^12.13.0 || ^14.15.0 || ^16.10.0 || >=17.0.0 }
hasBin: true
peerDependencies:
'@babel/core': '>=7.0.0-beta.0 <8'
'@jest/types': ^28.0.0
babel-jest: ^28.0.0
esbuild: '*'
jest: ^28.0.0
typescript: '>=4.3'
peerDependenciesMeta:
'@babel/core':
optional: true
babel-jest:
optional: true
esbuild:
optional: true
dependencies:
'@jest/types': 28.1.3
bs-logger: 0.2.6
fast-json-stable-stringify: 2.1.0
jest: 28.1.3
jest-util: 28.1.3
json5: 2.2.1
lodash.memoize: 4.1.2
make-error: 1.3.6
semver: 7.3.7
typescript: 4.5.2
yargs-parser: 21.0.1
dev: true
/ts-node-dev/1.1.8_typescript@4.7.3:
resolution:
{ integrity: sha512-Q/m3vEwzYwLZKmV6/0VlFxcZzVV/xcgOt+Tx/VjaaRHyiBcFlV0541yrT09QjzzCxlDZ34OzKjrFAynlmtflEg== }
@@ -15611,6 +15543,34 @@ packages:
fsevents: 2.3.2
dev: true
/vite/3.0.2:
resolution:
{ integrity: sha512-TAqydxW/w0U5AoL5AsD9DApTvGb2iNbGs3sN4u2VdT1GFkQVUfgUldt+t08TZgi23uIauh1TUOQJALduo9GXqw== }
engines: { node: ^14.18.0 || >=16.0.0 }
hasBin: true
peerDependencies:
less: '*'
sass: '*'
stylus: '*'
terser: ^5.4.0
peerDependenciesMeta:
less:
optional: true
sass:
optional: true
stylus:
optional: true
terser:
optional: true
dependencies:
esbuild: 0.14.49
postcss: 8.4.14
resolve: 1.22.1
rollup: 2.77.0
optionalDependencies:
fsevents: 2.3.2
dev: true
/vite/3.0.2_sass@1.43.4:
resolution:
{ integrity: sha512-TAqydxW/w0U5AoL5AsD9DApTvGb2iNbGs3sN4u2VdT1GFkQVUfgUldt+t08TZgi23uIauh1TUOQJALduo9GXqw== }
@@ -15640,6 +15600,46 @@ packages:
fsevents: 2.3.2
dev: true
/vitest/0.18.1:
resolution:
{ integrity: sha512-4F/1K/Vn4AvJwe7i2YblR02PT5vMKcw9KN4unDq2KD0YcSxX0B/6D6Qu9PJaXwVuxXMFTQ5ovd4+CQaW3bwofA== }
engines: { node: '>=v14.16.0' }
hasBin: true
peerDependencies:
'@edge-runtime/vm': '*'
'@vitest/ui': '*'
c8: '*'
happy-dom: '*'
jsdom: '*'
peerDependenciesMeta:
'@edge-runtime/vm':
optional: true
'@vitest/ui':
optional: true
c8:
optional: true
happy-dom:
optional: true
jsdom:
optional: true
dependencies:
'@types/chai': 4.3.1
'@types/chai-subset': 1.3.3
'@types/node': 18.0.4
chai: 4.3.6
debug: 4.3.4
local-pkg: 0.4.2
tinypool: 0.2.4
tinyspy: 1.0.0
vite: 3.0.2
transitivePeerDependencies:
- less
- sass
- stylus
- supports-color
- terser
dev: true
/vitest/0.18.1_zr7mwyjguy3vali3lbhmwsgtqy:
resolution:
{ integrity: sha512-4F/1K/Vn4AvJwe7i2YblR02PT5vMKcw9KN4unDq2KD0YcSxX0B/6D6Qu9PJaXwVuxXMFTQ5ovd4+CQaW3bwofA== }