Skip to content

Commit

Permalink
feat: migrate to native ESM (#84)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Drop support for Jest v26

BREAKING CHANGE: Module is now written in native ESM
  • Loading branch information
SimenB authored Sep 29, 2021
1 parent 1bcd08f commit a8e6940
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -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'],
};
5 changes: 1 addition & 4 deletions filename.js
Original file line number Diff line number Diff line change
@@ -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';
19 changes: 13 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"./testname": "./build/test_name_plugin/plugin.js",
"./package.json": "./package.json"
},
"type": "module",
"author": "Rogelio Guzman <rogelioguzmanh@gmail.com>",
"description": "Jest plugin for filtering by filename or test name",
"license": "MIT",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -66,9 +70,12 @@
"typescript": "^4.0.2"
},
"peerDependencies": {
"jest": "^26.0.0 || ^27.0.0"
"jest": "^27.0.0"
},
"jest": {
"extensionsToTreatAsEsm": [
".ts"
],
"watchPlugins": [
"<rootDir>/filename",
"<rootDir>/testname"
Expand Down
35 changes: 33 additions & 2 deletions src/file_name_plugin/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -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[] = [
{
Expand All @@ -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);
Expand Down
10 changes: 0 additions & 10 deletions src/lib/__mocks__/ansi-escapes.js

This file was deleted.

38 changes: 35 additions & 3 deletions src/test_name_plugin/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -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 = [
{
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/test_utils/pluginTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
5 changes: 1 addition & 4 deletions testname.js
Original file line number Diff line number Diff line change
@@ -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';
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
},
Expand Down
54 changes: 49 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit a8e6940

Please sign in to comment.