From afb34ff338f192ec1d36ae14f8ccccaa241892ec Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 28 Sep 2021 15:45:43 +0200 Subject: [PATCH] feat: migrate to native ESM BREAKING CHANGE: Drop support for Jest v26 BREAKING CHANGE: Module is now written in native ESM --- README.md | 2 +- babel.config.js | 18 +++++-- filename.js | 5 +- package.json | 19 ++++--- src/file_name_plugin/__tests__/plugin.test.ts | 35 +++++++++++- src/lib/__mocks__/ansi-escapes.js | 10 ---- src/test_name_plugin/__tests__/plugin.test.ts | 38 +++++++++++-- src/test_utils/pluginTester.ts | 1 + testname.js | 5 +- tsconfig.json | 3 +- yarn.lock | 54 +++++++++++++++++-- 11 files changed, 148 insertions(+), 42 deletions(-) delete mode 100644 src/lib/__mocks__/ansi-escapes.js diff --git a/README.md b/README.md index d449148..c07a0d3 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ ### Install -Install `jest`_(it needs Jest 26+)_ and `jest-watch-typeahead` +Install `jest`_(it needs Jest 27+)_ and `jest-watch-typeahead` ```bash yarn add --dev jest jest-watch-typeahead diff --git a/babel.config.js b/babel.config.js index 8705707..e5b0e65 100644 --- a/babel.config.js +++ b/babel.config.js @@ -1,19 +1,27 @@ -// eslint-disable-next-line @typescript-eslint/no-var-requires,import/no-extraneous-dependencies -const semver = require('semver'); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const pkg = require('./package.json'); +// eslint-disable-next-line import/no-extraneous-dependencies +import semver from 'semver'; +import { readFileSync } from 'fs'; + +let pkg = readFileSync('./package.json', 'utf8'); + +pkg = JSON.parse(pkg); const supportedNodeVersion = semver.minVersion(pkg.engines.node).version; -module.exports = { +export default { ignore: ['**/__mocks__/**'], presets: [ [ '@babel/preset-env', { + modules: false, targets: { node: supportedNodeVersion }, }, ], '@babel/preset-typescript', ], + plugins: + process.env.NODE_ENV === 'test' + ? [] + : ['babel-plugin-add-import-extension'], }; diff --git a/filename.js b/filename.js index 2ec8e80..de25051 100644 --- a/filename.js +++ b/filename.js @@ -1,4 +1 @@ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const FileNamePlugin = require('./build/file_name_plugin/plugin').default; - -module.exports = FileNamePlugin; +export { default } from './build/file_name_plugin/plugin.js'; diff --git a/package.json b/package.json index e21c07e..2a6783f 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "./testname": "./build/test_name_plugin/plugin.js", "./package.json": "./package.json" }, + "type": "module", "author": "Rogelio Guzman ", "description": "Jest plugin for filtering by filename or test name", "license": "MIT", @@ -22,7 +23,7 @@ "testname.js" ], "scripts": { - "test": "jest", + "test": "cross-env NODE_OPTIONS=\"--experimental-vm-modules\" jest", "lint": "eslint .", "prebuild": "rimraf build", "build": "babel --extensions .js,.ts src -d build && rimraf **/*.test.{js,ts},integration build/**/__tests__ build/test_utils", @@ -35,15 +36,16 @@ "chalk": "^4.0.0", "jest-regex-util": "^27.0.0", "jest-watcher": "^27.0.0", - "slash": "^3.0.0", - "string-length": "^4.0.1", - "strip-ansi": "^6.0.0" + "slash": "^4.0.0", + "string-length": "^5.0.1", + "strip-ansi": "^7.0.1" }, "devDependencies": { "@babel/cli": "^7.8.4", "@babel/core": "^7.9.6", "@babel/preset-env": "^7.9.6", "@babel/preset-typescript": "^7.10.4", + "@jest/globals": "^27.2.3", "@jest/types": "^27.0.0", "@semantic-release/changelog": "^5.0.1", "@semantic-release/git": "^9.0.0", @@ -52,12 +54,14 @@ "@typescript-eslint/eslint-plugin": "^4.0.1", "@typescript-eslint/parser": "^4.0.1", "babel-jest": "^27.0.0", + "babel-plugin-add-import-extension": "^1.6.0", + "cross-env": "^7.0.3", "eslint": "^7.8.1", "eslint-config-airbnb-base": "^14.1.0", "eslint-config-prettier": "^8.0.0", "eslint-plugin-import": "^2.20.2", "eslint-plugin-jest": "^24.0.0", - "eslint-plugin-prettier": "^3.1.3", + "eslint-plugin-prettier": "^4.0.0", "jest": "^27.0.0", "prettier": "^2.1.1", "rimraf": "^3.0.2", @@ -66,9 +70,12 @@ "typescript": "^4.0.2" }, "peerDependencies": { - "jest": "^26.0.0 || ^27.0.0" + "jest": "^27.0.0" }, "jest": { + "extensionsToTreatAsEsm": [ + ".ts" + ], "watchPlugins": [ "/filename", "/testname" diff --git a/src/file_name_plugin/__tests__/plugin.test.ts b/src/file_name_plugin/__tests__/plugin.test.ts index afdcbc7..550eb31 100644 --- a/src/file_name_plugin/__tests__/plugin.test.ts +++ b/src/file_name_plugin/__tests__/plugin.test.ts @@ -1,7 +1,33 @@ import { KEYS } from 'jest-watcher'; import type { Config } from '@jest/types'; -import pluginTester from '../../test_utils/pluginTester'; -import FileNamePlugin from '../plugin'; +import { jest } from '@jest/globals'; + +let pluginTester: typeof import('../../test_utils/pluginTester').default = null; +let FileNamePlugin: typeof import('../plugin').default = null; + +jest.unstable_mockModule('ansi-escapes', () => ({ + default: { + clearScreen: '[MOCK - clearScreen]', + cursorDown: (count = 1) => `[MOCK - cursorDown(${count})]`, + cursorLeft: '[MOCK - cursorLeft]', + cursorHide: '[MOCK - cursorHide]', + cursorRestorePosition: '[MOCK - cursorRestorePosition]', + cursorSavePosition: '[MOCK - cursorSavePosition]', + cursorShow: '[MOCK - cursorShow]', + cursorTo: (x, y) => `[MOCK - cursorTo(${x}, ${y})]`, + }, +})); + +jest.doMock('ansi-escapes', () => ({ + clearScreen: '[MOCK - clearScreen]', + cursorDown: (count = 1) => `[MOCK - cursorDown(${count})]`, + cursorLeft: '[MOCK - cursorLeft]', + cursorHide: '[MOCK - cursorHide]', + cursorRestorePosition: '[MOCK - cursorRestorePosition]', + cursorSavePosition: '[MOCK - cursorSavePosition]', + cursorShow: '[MOCK - cursorShow]', + cursorTo: (x, y) => `[MOCK - cursorTo(${x}, ${y})]`, +})); const projects: Config.ProjectConfig[] = [ { @@ -18,6 +44,11 @@ const projects: Config.ProjectConfig[] = [ }, ]; +beforeAll(async () => { + FileNamePlugin = (await import('../plugin')).default; + pluginTester = (await import('../../test_utils/pluginTester')).default; +}); + it('shows the correct initial state', async () => { const { stdout, hookEmitter, updateConfigAndRun, plugin, type } = pluginTester(FileNamePlugin); diff --git a/src/lib/__mocks__/ansi-escapes.js b/src/lib/__mocks__/ansi-escapes.js deleted file mode 100644 index c7f1ef3..0000000 --- a/src/lib/__mocks__/ansi-escapes.js +++ /dev/null @@ -1,10 +0,0 @@ -module.exports = { - clearScreen: '[MOCK - clearScreen]', - cursorDown: (count = 1) => `[MOCK - cursorDown(${count})]`, - cursorLeft: '[MOCK - cursorLeft]', - cursorHide: '[MOCK - cursorHide]', - cursorRestorePosition: '[MOCK - cursorRestorePosition]', - cursorSavePosition: '[MOCK - cursorSavePosition]', - cursorShow: '[MOCK - cursorShow]', - cursorTo: (x, y) => `[MOCK - cursorTo(${x}, ${y})]`, -}; diff --git a/src/test_name_plugin/__tests__/plugin.test.ts b/src/test_name_plugin/__tests__/plugin.test.ts index ca8c693..a26cae1 100644 --- a/src/test_name_plugin/__tests__/plugin.test.ts +++ b/src/test_name_plugin/__tests__/plugin.test.ts @@ -1,6 +1,32 @@ -import { KEYS } from 'jest-watcher'; -import pluginTester from '../../test_utils/pluginTester'; -import TestNamePlugin from '../plugin'; +import { jest } from '@jest/globals'; + +let pluginTester: typeof import('../../test_utils/pluginTester').default = null; +let TestNamePlugin: typeof import('../plugin').default = null; +let KEYS: typeof import('jest-watcher').KEYS = null; + +jest.unstable_mockModule('ansi-escapes', () => ({ + default: { + clearScreen: '[MOCK - clearScreen]', + cursorDown: (count = 1) => `[MOCK - cursorDown(${count})]`, + cursorLeft: '[MOCK - cursorLeft]', + cursorHide: '[MOCK - cursorHide]', + cursorRestorePosition: '[MOCK - cursorRestorePosition]', + cursorSavePosition: '[MOCK - cursorSavePosition]', + cursorShow: '[MOCK - cursorShow]', + cursorTo: (x, y) => `[MOCK - cursorTo(${x}, ${y})]`, + }, +})); + +jest.doMock('ansi-escapes', () => ({ + clearScreen: '[MOCK - clearScreen]', + cursorDown: (count = 1) => `[MOCK - cursorDown(${count})]`, + cursorLeft: '[MOCK - cursorLeft]', + cursorHide: '[MOCK - cursorHide]', + cursorRestorePosition: '[MOCK - cursorRestorePosition]', + cursorSavePosition: '[MOCK - cursorSavePosition]', + cursorShow: '[MOCK - cursorShow]', + cursorTo: (x, y) => `[MOCK - cursorTo(${x}, ${y})]`, +})); const testResults = [ { @@ -17,6 +43,12 @@ const testResults = [ }, ]; +beforeAll(async () => { + TestNamePlugin = (await import('../plugin')).default; + pluginTester = (await import('../../test_utils/pluginTester')).default; + KEYS = (await import('jest-watcher')).KEYS; +}); + it('shows the correct initial state', async () => { const { stdout, updateConfigAndRun, plugin, type } = pluginTester(TestNamePlugin); diff --git a/src/test_utils/pluginTester.ts b/src/test_utils/pluginTester.ts index e2ea85e..e26cb84 100644 --- a/src/test_utils/pluginTester.ts +++ b/src/test_utils/pluginTester.ts @@ -5,6 +5,7 @@ import { UpdateConfigCallback, } from 'jest-watcher'; import stripAnsi from 'strip-ansi'; +import { jest } from '@jest/globals'; import type FileNamePlugin from '../file_name_plugin/plugin'; import type TestNamePlugin from '../test_name_plugin/plugin'; import type { PluginConfig } from '../types/Config'; diff --git a/testname.js b/testname.js index d4fdbd0..9194222 100644 --- a/testname.js +++ b/testname.js @@ -1,4 +1 @@ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const TestNamePlugin = require('./build/test_name_plugin/plugin').default; - -module.exports = TestNamePlugin; +export { default } from './build/test_name_plugin/plugin.js'; diff --git a/tsconfig.json b/tsconfig.json index 6474fbc..f1ffa99 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,14 +1,13 @@ { "compilerOptions": { "target": "es2015", - "module": "commonjs", + "module": "ES2020", "moduleResolution": "node", "lib": ["es2018"], "noEmit": true, "noImplicitReturns": true, "strict": true, "esModuleInterop": true, - "resolveJsonModule": true, "forceConsistentCasingInFileNames": true, "importsNotUsedAsValues": "error" }, diff --git a/yarn.lock b/yarn.lock index ec3dd00..7cab67f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1866,6 +1866,11 @@ ansi-regex@^5.0.1: resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.1.tgz#082cb2c89c9fe8659a311a53bd6a4dc5301db304" integrity sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ== +ansi-regex@^6.0.1: + version "6.0.1" + resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-6.0.1.tgz#3183e38fae9a65d7cb5e53945cd5897d0260a06a" + integrity sha512-n5M855fKb2SsfMIiFFoVrABHJC8QtHwVx+mHWP3QcEqBHYienj5dHSgjbxtC0WEZXYt4wcD6zrQElDPhFuZgfA== + ansi-styles@^3.2.1: version "3.2.1" resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d" @@ -2037,6 +2042,13 @@ babel-jest@^27.0.0, babel-jest@^27.2.3: graceful-fs "^4.2.4" slash "^3.0.0" +babel-plugin-add-import-extension@^1.6.0: + version "1.6.0" + resolved "https://registry.yarnpkg.com/babel-plugin-add-import-extension/-/babel-plugin-add-import-extension-1.6.0.tgz#807ce65b38d4763797c1616cb4e8372da167cdd1" + integrity sha512-JVSQPMzNzN/S4wPRoKQ7+u8PlkV//BPUMnfWVbr63zcE+6yHdU2Mblz10Vf7qe+6Rmu4svF5jG7JxdcPi9VvKg== + dependencies: + "@babel/helper-plugin-utils" "^7.14.5" + babel-plugin-dynamic-import-node@^2.3.3: version "2.3.3" resolved "https://registry.yarnpkg.com/babel-plugin-dynamic-import-node/-/babel-plugin-dynamic-import-node-2.3.3.tgz#84fda19c976ec5c6defef57f9427b3def66e17a3" @@ -2298,6 +2310,11 @@ char-regex@^1.0.2: resolved "https://registry.yarnpkg.com/char-regex/-/char-regex-1.0.2.tgz#d744358226217f981ed58f479b1d6bcc29545dcf" integrity sha512-kWWXztvZ5SBQV+eRgKFeh8q5sLuZY2+8WUIzlxWVTg+oGwY14qylx1KbKzHd8P6ZYkAg0xyIDU9JMHhyJMZ1jw== +char-regex@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/char-regex/-/char-regex-2.0.0.tgz#16f98f3f874edceddd300fda5d58df380a7641a6" + integrity sha512-oGu2QekBMXgyQNWPDRQ001bjvDnZe4/zBTz37TMbiKz1NbNiyiH5hRkobe7npRN6GfbGbxMYFck/vQ1r9c1VMA== + chokidar@^3.4.0: version "3.5.2" resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.2.tgz#dba3976fcadb016f66fd365021d91600d01c1e75" @@ -2556,7 +2573,14 @@ cosmiconfig@^7.0.0: path-type "^4.0.0" yaml "^1.10.0" -cross-spawn@^7.0.0, cross-spawn@^7.0.2, cross-spawn@^7.0.3: +cross-env@^7.0.3: + version "7.0.3" + resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-7.0.3.tgz#865264b29677dc015ba8418918965dd232fc54cf" + integrity sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw== + dependencies: + cross-spawn "^7.0.1" + +cross-spawn@^7.0.0, cross-spawn@^7.0.1, cross-spawn@^7.0.2, cross-spawn@^7.0.3: version "7.0.3" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6" integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w== @@ -2977,10 +3001,10 @@ eslint-plugin-jest@^24.0.0: dependencies: "@typescript-eslint/experimental-utils" "^4.0.1" -eslint-plugin-prettier@^3.1.3: - version "3.4.1" - resolved "https://registry.yarnpkg.com/eslint-plugin-prettier/-/eslint-plugin-prettier-3.4.1.tgz#e9ddb200efb6f3d05ffe83b1665a716af4a387e5" - integrity sha512-htg25EUYUeIhKHXjOinK4BgCcDwtLHjqaxCDsMy5nbnUMkKFvIhMVCp+5GFUXQ4Nr8lBsPqtGAqBenbpFqAA2g== +eslint-plugin-prettier@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/eslint-plugin-prettier/-/eslint-plugin-prettier-4.0.0.tgz#8b99d1e4b8b24a762472b4567992023619cb98e0" + integrity sha512-98MqmCJ7vJodoQK359bqQWaxOE0CS8paAz/GgjaZLyex4TTk3g9HugoO89EqWCrFiOqn9EVvcoo7gZzONCWVwQ== dependencies: prettier-linter-helpers "^1.0.0" @@ -6260,6 +6284,11 @@ slash@^3.0.0: resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634" integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q== +slash@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/slash/-/slash-4.0.0.tgz#2422372176c4c6c5addb5e2ada885af984b396a7" + integrity sha512-3dOsAHXXUkQTpOYcoAxLIorMTp4gIQr5IW3iVb7A7lFIp0VHhnynm9izx6TssdrIcVIESAlVjtnO2K8bg+Coew== + slice-ansi@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/slice-ansi/-/slice-ansi-4.0.0.tgz#500e8dd0fd55b05815086255b3195adf2a45fe6b" @@ -6416,6 +6445,14 @@ string-length@^4.0.1: char-regex "^1.0.2" strip-ansi "^6.0.0" +string-length@^5.0.1: + version "5.0.1" + resolved "https://registry.yarnpkg.com/string-length/-/string-length-5.0.1.tgz#3d647f497b6e8e8d41e422f7e0b23bc536c8381e" + integrity sha512-9Ep08KAMUn0OadnVaBuRdE2l615CQ508kr0XMadjClfYpdCyvrbFp6Taebo8yyxokQ4viUd/xPPUA4FGgUa0ow== + dependencies: + char-regex "^2.0.0" + strip-ansi "^7.0.1" + string-width@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/string-width/-/string-width-1.0.2.tgz#118bdf5b8cdc51a2a7e70d211e07e2b0b9b107d3" @@ -6498,6 +6535,13 @@ strip-ansi@^6.0.0, strip-ansi@^6.0.1: dependencies: ansi-regex "^5.0.1" +strip-ansi@^7.0.1: + version "7.0.1" + resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.0.1.tgz#61740a08ce36b61e50e65653f07060d000975fb2" + integrity sha512-cXNxvT8dFNRVfhVME3JAe98mkXDYN2O1l7jmcwMnOslDeESg1rF/OZMtK0nRAhiari1unG5cD4jG3rapUAkLbw== + dependencies: + ansi-regex "^6.0.1" + strip-bom@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-3.0.0.tgz#2334c18e9c759f7bdd56fdef7e9ae3d588e68ed3"