From 08c5ce10f793dafacf343651c0681dfd34b9ac94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20S=C3=A1nchez=20Jim=C3=A9nez?= Date: Sat, 18 Sep 2021 14:42:03 +0200 Subject: [PATCH] test: hacking cjs tests chore: modify target and module on tsconfig configuration --- jest.cjs.config.ts | 17 +++++++++++------ jest.esm.config.ts | 20 ++++++++++---------- jest/jest.shared.config.ts | 27 +++++++++++++-------------- package.json | 2 +- src/loadCollection.cjs.test.ts | 29 +++-------------------------- src/loadCollection.esm.test.ts | 29 +++-------------------------- src/loadCollection.test.ts | 32 ++++++++++++++++++++++++++++++++ src/locate.cjs.test.ts | 10 ++-------- src/locate.esm.test.ts | 10 ++-------- src/locate.test.ts | 12 ++++++++++++ tsconfig.json | 5 ++--- 11 files changed, 91 insertions(+), 102 deletions(-) create mode 100644 src/loadCollection.test.ts create mode 100644 src/locate.test.ts diff --git a/jest.cjs.config.ts b/jest.cjs.config.ts index 734df2a..416e6ce 100644 --- a/jest.cjs.config.ts +++ b/jest.cjs.config.ts @@ -1,7 +1,12 @@ -import config from './jest/jest.shared.config' +import { buildConfiguration } from './jest/jest.shared.config' -config.testMatch = [ - '**/?(*.)+(cjs).+(spec|test).+(ts|tsx|js)', -] - -export default config +export default buildConfiguration({ + testMatch: [ + '**/?(*.)+(cjs).+(spec|test).+(ts|tsx|js)', + ], + globals: { + 'ts-jest': { + useESM: false, + }, + }, +}) diff --git a/jest.esm.config.ts b/jest.esm.config.ts index c013a2e..9e3ae43 100644 --- a/jest.esm.config.ts +++ b/jest.esm.config.ts @@ -1,12 +1,12 @@ -import config from './jest/jest.shared.config' +import { buildConfiguration } from './jest/jest.shared.config' -config.testMatch = [ - '**/?(*.)+(esm).+(spec|test).+(ts|tsx|js)', -] -config.globals = { - 'ts-jest': { - useESM: true, +export default buildConfiguration({ + testMatch: [ + '**/?(*.)+(esm).+(spec|test).+(ts|tsx|js)', + ], + globals: { + 'ts-jest': { + useESM: true, + }, }, -} - -export default config +}) diff --git a/jest/jest.shared.config.ts b/jest/jest.shared.config.ts index 4fc8545..afaef44 100644 --- a/jest/jest.shared.config.ts +++ b/jest/jest.shared.config.ts @@ -2,18 +2,17 @@ import type { Config } from '@jest/types' // see https://jestjs.io/docs/ecmascript-modules -// Sync object -const config: Config.InitialOptions = { - verbose: true, - moduleDirectories: [ - 'node_modules', - 'src', - ], - moduleFileExtensions: ['ts', 'js'], - extensionsToTreatAsEsm: ['.ts'], - transform: { - '^.+\\.ts$': 'ts-jest', - }, +export const buildConfiguration = (configuration: Partial): Config.InitialOptions => { + return Object.assign({}, { + verbose: true, + moduleDirectories: [ + 'node_modules', + 'src', + ], + moduleFileExtensions: ['ts', 'js'], + extensionsToTreatAsEsm: ['.ts'], + transform: { + '^.+\\.ts$': 'ts-jest', + }, + }, configuration) } - -export default config diff --git a/package.json b/package.json index 9d5ba2b..8ba8eee 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "import": "./dist/index.mjs" } }, - "files": [ + "files": [ "dist", "json", "lib", diff --git a/src/loadCollection.cjs.test.ts b/src/loadCollection.cjs.test.ts index 1088602..d02a70f 100644 --- a/src/loadCollection.cjs.test.ts +++ b/src/loadCollection.cjs.test.ts @@ -1,27 +1,4 @@ -import { IconifyMetaDataCollection, lookupCollection, lookupCollections } from '../dist' +import { lookupCollection, lookupCollections } from '../dist' +import { lookupCollectionTest } from './loadCollection.test' -let collections: IconifyMetaDataCollection - -describe('lookupCollection and lookupCollections', () => { - beforeAll(() => { - return lookupCollections().then((c) => { - collections = c - return Promise.resolve(c) - }) - }) - test('lookupCollections has data', () => { - expect(collections ? Object.keys(collections).length : 0).toBeGreaterThan(0) - }) - test('mdi collection has prefix mdi', async() => { - const collection = await lookupCollection('mdi') - expect(collection.prefix).toBe('mdi') - }) - test('websymbol collection has prefix websymbol', async() => { - const collection = await lookupCollection('websymbol') - expect(collection.prefix).toBe('websymbol') - }) - test('fa collection has prefix fa', async() => { - const collection = await lookupCollection('fa') - expect(collection.prefix).toBe('fa') - }) -}) +lookupCollectionTest(lookupCollection, lookupCollections) diff --git a/src/loadCollection.esm.test.ts b/src/loadCollection.esm.test.ts index 4a7ced9..c67143c 100644 --- a/src/loadCollection.esm.test.ts +++ b/src/loadCollection.esm.test.ts @@ -1,27 +1,4 @@ -import { IconifyMetaDataCollection, lookupCollection, lookupCollections } from '.' +import { lookupCollectionTest } from './loadCollection.test' +import { lookupCollection, lookupCollections } from '.' -let collections: IconifyMetaDataCollection - -describe('lookupCollection and lookupCollections', () => { - beforeAll(() => { - return lookupCollections().then((c) => { - collections = c - return Promise.resolve(c) - }) - }) - test('lookupCollections has data', () => { - expect(collections ? Object.keys(collections).length : 0).toBeGreaterThan(0) - }) - test('mdi collection has prefix mdi', async() => { - const collection = await lookupCollection('mdi') - expect(collection.prefix).toBe('mdi') - }) - test('websymbol collection has prefix websymbol', async() => { - const collection = await lookupCollection('websymbol') - expect(collection.prefix).toBe('websymbol') - }) - test('fa collection has prefix fa', async() => { - const collection = await lookupCollection('fa') - expect(collection.prefix).toBe('fa') - }) -}) +lookupCollectionTest(lookupCollection, lookupCollections) diff --git a/src/loadCollection.test.ts b/src/loadCollection.test.ts new file mode 100644 index 0000000..b096b05 --- /dev/null +++ b/src/loadCollection.test.ts @@ -0,0 +1,32 @@ +import type { IconifyMetaDataCollection } from '.' +import { IconifyJSON } from '.' + +export const lookupCollectionTest = ( + lookupCollection: (name: string) => Promise, + lookupCollections: () => Promise, +) => { + let collections: IconifyMetaDataCollection + describe('lookupCollection and lookupCollections', () => { + beforeAll(() => { + return lookupCollections().then((c) => { + collections = c + return Promise.resolve(c) + }) + }) + test('lookupCollections has data', () => { + expect(collections ? Object.keys(collections).length : 0).toBeGreaterThan(0) + }) + test('mdi collection has prefix mdi', async() => { + const collection = await lookupCollection('mdi') + expect(collection.prefix).toBe('mdi') + }) + test('websymbol collection has prefix websymbol', async() => { + const collection = await lookupCollection('websymbol') + expect(collection.prefix).toBe('websymbol') + }) + test('fa collection has prefix fa', async() => { + const collection = await lookupCollection('fa') + expect(collection.prefix).toBe('fa') + }) + }) +} diff --git a/src/locate.cjs.test.ts b/src/locate.cjs.test.ts index 6c262c7..3cb9633 100644 --- a/src/locate.cjs.test.ts +++ b/src/locate.cjs.test.ts @@ -1,10 +1,4 @@ -import { resolve } from 'path' import { locate } from '../dist' +import { locateTest } from './locate.test' -const cwd = process.cwd() - -test('mdi resolves the json collection', () => { - const received = locate('mdi') as string - const expected = resolve(cwd, 'json', 'mdi.json').replace(/\\/g, '/') - expect(received).toBe(expected) -}) +locateTest(locate) diff --git a/src/locate.esm.test.ts b/src/locate.esm.test.ts index b63769c..abec92d 100644 --- a/src/locate.esm.test.ts +++ b/src/locate.esm.test.ts @@ -1,10 +1,4 @@ -import { resolve } from 'path' +import { locateTest } from './locate.test' import { locate } from '.' -const cwd = process.cwd() - -test('mdi resolves the json collection', () => { - const received = locate('mdi') as string - const expected = resolve(cwd, 'json', 'mdi.json').replace(/\\/g, '/') - expect(received).toBe(expected) -}) +locateTest(locate) diff --git a/src/locate.test.ts b/src/locate.test.ts new file mode 100644 index 0000000..f4f9c8f --- /dev/null +++ b/src/locate.test.ts @@ -0,0 +1,12 @@ +import { resolve } from 'path' +import { PathLike } from 'fs' + +const cwd = process.cwd() + +export const locateTest = (locate: (name: string) => PathLike) => { + test('mdi resolves the json collection', () => { + const received = locate('mdi') as string + const expected = resolve(cwd, 'json', 'mdi.json').replace(/\\/g, '/') + expect(received).toBe(expected) + }) +} diff --git a/tsconfig.json b/tsconfig.json index 685e220..cd5270e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,8 +1,7 @@ { "compilerOptions": { - "baseUrl": ".", - "module": "esnext", - "target": "ESNext", + "target": "es2017", + "module": "ESNext", "lib": ["ESNext"], "esModuleInterop": true, "strict": true,