diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml index 6f91954..0086358 100644 --- a/.github/ISSUE_TEMPLATE/config.yml +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -1,8 +1 @@ blank_issues_enabled: true -contact_links: - - name: AVA on Spectrum - url: https://spectrum.chat/ava - about: Ask questions and discuss in our Spectrum community - - name: Stack Overflow - url: https://stackoverflow.com/questions/tagged/ava - about: Tag your question on Stack Overflow diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4c247ee..c336669 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,7 +2,7 @@ name: Install and test @ava/typescript on: push: branches: - - master + - main pull_request: paths-ignore: - '*.md' @@ -13,15 +13,17 @@ jobs: strategy: fail-fast: false matrix: - node-version: [^12.22, ^14.16, ^15] + node-version: [^12.22, ^14.17, ^16.4, ^17] os: [ubuntu-latest, windows-latest] steps: - - uses: actions/checkout@v1 - with: - fetch-depth: 1 - - uses: actions/setup-node@v1 + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} + - run: npm install --global npm@8 - run: npm install --no-audit - run: npm test - - uses: codecov/codecov-action@v1 + - uses: codecov/codecov-action@v2 + with: + files: coverage/lcov.info + name: ${{ matrix.os }}/${{ matrix.node-version }} diff --git a/README.md b/README.md index 4466038..c0452b5 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,10 @@ Output files are expected to have the `.js` extension. AVA searches your entire project for `*.js`, `*.cjs`, `*.mjs` and `*.ts` files (or other extensions you've configured). It will ignore such files found in the `rewritePaths` targets (e.g. `build/`). If you use more specific paths, for instance `build/main/`, you may need to change AVA's `files` configuration to ignore other directories. +## ES Modules + +When used with AVA 4, if your `package.json` has configured `"type": "module"`, or you've configured AVA to treat the `js` extension as `module`, then `@ava/typescript` will import the output file as an ES module. Note that this is based on the *output file*, not the `ts` extension. + ## Add additional extensions You can configure AVA to recognize additional file extensions. To add (partial†) JSX support: diff --git a/index.js b/index.js index 8fc5369..d27d1b0 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,10 @@ -'use strict'; -const path = require('path'); -const escapeStringRegexp = require('escape-string-regexp'); -const execa = require('execa'); -const pkg = require('./package.json'); +import fs from 'node:fs'; +import path from 'node:path'; +import {pathToFileURL} from 'node:url'; +import escapeStringRegexp from 'escape-string-regexp'; +import execa from 'execa'; +const pkg = fs.readFileSync(new URL('package.json', import.meta.url)); const help = `See https://github.com/avajs/typescript/blob/v${pkg.version}/README.md`; function isPlainObject(x) { @@ -44,7 +45,7 @@ const configProperties = { required: true, isValid(compile) { return compile === false || compile === 'tsc'; - } + }, }, rewritePaths: { required: true, @@ -53,23 +54,21 @@ const configProperties = { return false; } - return Object.entries(rewritePaths).every(([from, to]) => { - return from.endsWith('/') && typeof to === 'string' && to.endsWith('/'); - }); - } + return Object.entries(rewritePaths).every(([from, to]) => from.endsWith('/') && typeof to === 'string' && to.endsWith('/')); + }, }, extensions: { required: false, isValid(extensions) { - return Array.isArray(extensions) && - extensions.length > 0 && - extensions.every(ext => typeof ext === 'string' && ext !== '') && - new Set(extensions).size === extensions.length; - } - } + return Array.isArray(extensions) + && extensions.length > 0 + && extensions.every(ext => typeof ext === 'string' && ext !== '') + && new Set(extensions).size === extensions.length; + }, + }, }; -module.exports = ({negotiateProtocol}) => { +export default function typescriptProvider({negotiateProtocol}) { const protocol = negotiateProtocol(['ava-3.2'], {version: pkg.version}); if (protocol === null) { return; @@ -86,12 +85,12 @@ module.exports = ({negotiateProtocol}) => { const { extensions = ['ts'], rewritePaths: relativeRewritePaths, - compile + compile, } = config; const rewritePaths = Object.entries(relativeRewritePaths).map(([from, to]) => [ path.join(protocol.projectDir, from), - path.join(protocol.projectDir, to) + path.join(protocol.projectDir, to), ]); const testFileExtension = new RegExp(`\\.(${extensions.map(ext => escapeStringRegexp(ext)).join('|')})$`); @@ -102,13 +101,13 @@ module.exports = ({negotiateProtocol}) => { } return { - extensions: extensions.slice(), - rewritePaths: rewritePaths.slice() + extensions: [...extensions], + rewritePaths: [...rewritePaths], }; }, get extensions() { - return extensions.slice(); + return [...extensions]; }, ignoreChange(filePath) { @@ -139,18 +138,19 @@ module.exports = ({negotiateProtocol}) => { filePatterns: [ ...filePatterns, '!**/*.d.ts', - ...Object.values(relativeRewritePaths).map(to => `!${to}**`) + ...Object.values(relativeRewritePaths).map(to => `!${to}**`), ], ignoredByWatcherPatterns: [ ...ignoredByWatcherPatterns, - ...Object.values(relativeRewritePaths).map(to => `${to}**/*.js.map`) - ] + ...Object.values(relativeRewritePaths).map(to => `${to}**/*.js.map`), + ], }; - } + }, }; }, worker({extensionsToLoadAsModules, state: {extensions, rewritePaths}}) { + const useImport = extensionsToLoadAsModules.includes('js'); const testFileExtension = new RegExp(`\\.(${extensions.map(ext => escapeStringRegexp(ext)).join('|')})$`); return { @@ -159,18 +159,12 @@ module.exports = ({negotiateProtocol}) => { }, async load(ref, {requireFn}) { - for (const extension of extensionsToLoadAsModules) { - if (ref.endsWith(`.${extension}`)) { - throw new Error('@ava/typescript cannot yet load ESM files'); - } - } - const [from, to] = rewritePaths.find(([from]) => ref.startsWith(from)); // TODO: Support JSX preserve mode — https://www.typescriptlang.org/docs/handbook/jsx.html const rewritten = `${to}${ref.slice(from.length)}`.replace(testFileExtension, '.js'); - return requireFn(rewritten); - } + return useImport ? import(pathToFileURL(rewritten)) : requireFn(rewritten); // eslint-disable-line node/no-unsupported-features/es-syntax + }, }; - } + }, }; -}; +} diff --git a/package.json b/package.json index c88c33c..35fb797 100644 --- a/package.json +++ b/package.json @@ -3,11 +3,15 @@ "version": "2.0.0", "description": "TypeScript provider for AVA", "engines": { - "node": ">=12.22 <13 || >=14.16 <15 || >=15" + "node": ">=12.22 <13 || >=14.17 <15 || >=16.4 <17 || >=17" }, "files": [ "index.js" ], + "exports": { + ".": "./index.js" + }, + "type": "module", "author": "Mark Wubben (https://novemberborn.net)", "repository": "avajs/typescript", "license": "MIT", @@ -19,15 +23,15 @@ "test": "xo && c8 ava" }, "dependencies": { - "escape-string-regexp": "^4.0.0", - "execa": "^5.0.0" + "escape-string-regexp": "^5.0.0", + "execa": "^5.1.1" }, "devDependencies": { - "ava": "^3.15.0", - "c8": "^7.7.1", + "ava": "4.0.0-rc.1", + "c8": "^7.10.0", "del": "^6.0.0", - "typescript": "^4.2.4", - "xo": "^0.38.2" + "typescript": "^4.4.4", + "xo": "^0.46.3" }, "c8": { "reporter": [ @@ -40,14 +44,15 @@ "files": [ "!test/broken-fixtures/**" ], + "ignoredByWatcher": [ + "test/fixtures/**", + "test/broken-fixtures/**" + ], "timeout": "60s" }, "xo": { "ignores": [ "test/broken-fixtures" - ], - "rules": { - "import/order": "off" - } + ] } } diff --git a/test/_with-provider.js b/test/_with-provider.js index 9bc8089..673be3f 100644 --- a/test/_with-provider.js +++ b/test/_with-provider.js @@ -1,23 +1,25 @@ -const path = require('path'); -const pkg = require('../package.json'); -const makeProvider = require('..'); +import fs from 'node:fs'; +import path from 'node:path'; +import {fileURLToPath} from 'node:url'; +import makeProvider from '@ava/typescript'; -const createProviderMacro = (identifier, avaVersion, projectDir = __dirname) => { - return (t, run) => run(t, makeProvider({ - negotiateProtocol(identifiers, {version}) { - t.true(identifiers.includes(identifier)); - t.is(version, pkg.version); - return { - ava: {avaVersion}, - identifier, - normalizeGlobPatterns: patterns => patterns, - async findFiles({patterns}) { - return patterns.map(file => path.join(projectDir, file)); - }, - projectDir - }; - } - })); -}; +const __dirname = path.dirname(fileURLToPath(import.meta.url)); +const pkg = fs.readFileSync(new URL('../package.json', import.meta.url)); -module.exports = createProviderMacro; +const createProviderMacro = (identifier, avaVersion, projectDir = __dirname) => (t, run) => run(t, makeProvider({ + negotiateProtocol(identifiers, {version}) { + t.true(identifiers.includes(identifier)); + t.is(version, pkg.version); + return { + ava: {avaVersion}, + identifier, + normalizeGlobPatterns: patterns => patterns, + async findFiles({patterns}) { + return patterns.map(file => path.join(projectDir, file)); + }, + projectDir, + }; + }, +})); + +export default createProviderMacro; diff --git a/test/base.js b/test/base.js index d3344e5..1fb0df1 100644 --- a/test/base.js +++ b/test/base.js @@ -1,5 +1,5 @@ -const test = require('ava'); -const makeProvider = require('..'); +import test from 'ava'; +import makeProvider from '@ava/typescript'; test('bails when negotiating protocol returns `null`', t => { const provider = makeProvider({negotiateProtocol: () => null}); diff --git a/test/compilation.js b/test/compilation.js index 0a791dd..6cf9257 100644 --- a/test/compilation.js +++ b/test/compilation.js @@ -1,9 +1,11 @@ -const path = require('path'); -const test = require('ava'); -const del = require('del'); -const execa = require('execa'); -const createProviderMacro = require('./_with-provider'); - +import path from 'node:path'; +import {fileURLToPath} from 'node:url'; +import test from 'ava'; +import del from 'del'; +import execa from 'execa'; +import createProviderMacro from './_with-provider.js'; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const withProvider = createProviderMacro('ava-3.2', '3.2.0', path.join(__dirname, 'fixtures')); const withAltProvider = createProviderMacro('ava-3.2', '3.2.0', path.join(__dirname, 'broken-fixtures')); @@ -12,26 +14,24 @@ test.before('deleting compiled files', async t => { t.log(await del('test/broken-fixtures/typescript/compiled')); }); -const compile = async provider => { - return { - state: await provider.main({ - config: { - rewritePaths: { - 'ts/': 'typescript/', - 'compiled/': 'typescript/compiled/' - }, - compile: 'tsc' - } - }).compile() - }; -}; +const compile = async provider => ({ + state: await provider.main({ + config: { + rewritePaths: { + 'ts/': 'typescript/', + 'compiled/': 'typescript/compiled/', + }, + compile: 'tsc', + }, + }).compile(), +}); test('worker(): load rewritten paths files', withProvider, async (t, provider) => { const {state} = await compile(provider); const {stdout, stderr} = await execa.node( path.join(__dirname, 'fixtures/install-and-load'), - [JSON.stringify(state), path.join(__dirname, 'fixtures/ts', 'file.ts')], - {cwd: path.join(__dirname, 'fixtures')} + [JSON.stringify({state}), path.join(__dirname, 'fixtures/ts', 'file.ts')], + {cwd: path.join(__dirname, 'fixtures')}, ); if (stderr.length > 0) { t.log(stderr); @@ -44,8 +44,8 @@ test('worker(): runs compiled files', withProvider, async (t, provider) => { const {state} = await compile(provider); const {stdout, stderr} = await execa.node( path.join(__dirname, 'fixtures/install-and-load'), - [JSON.stringify(state), path.join(__dirname, 'fixtures/compiled', 'index.ts')], - {cwd: path.join(__dirname, 'fixtures')} + [JSON.stringify({state}), path.join(__dirname, 'fixtures/compiled', 'index.ts')], + {cwd: path.join(__dirname, 'fixtures')}, ); if (stderr.length > 0) { t.log(stderr); diff --git a/test/esm.js b/test/esm.js new file mode 100644 index 0000000..4d9d5e3 --- /dev/null +++ b/test/esm.js @@ -0,0 +1,33 @@ +import path from 'node:path'; +import {fileURLToPath} from 'node:url'; +import test from 'ava'; +import execa from 'execa'; +import createProviderMacro from './_with-provider.js'; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); +const withProvider = createProviderMacro('ava-3.2', '3.2.0', path.join(__dirname, 'fixtures')); + +const setup = async provider => ({ + state: await provider.main({ + config: { + rewritePaths: { + 'esm/': 'esm/', + }, + compile: false, + }, + }).compile(), +}); + +test('worker(): import ESM', withProvider, async (t, provider) => { + const {state} = await setup(provider); + const {stdout, stderr} = await execa.node( + path.join(__dirname, 'fixtures/install-and-load'), + [JSON.stringify({extensionsToLoadAsModules: ['js'], state}), path.join(__dirname, 'fixtures/esm', 'index.ts')], + {cwd: path.join(__dirname, 'fixtures')}, + ); + if (stderr.length > 0) { + t.log(stderr); + } + + t.snapshot(stdout); +}); diff --git a/test/fixtures/esm/index.js b/test/fixtures/esm/index.js new file mode 100644 index 0000000..b8a2e5c --- /dev/null +++ b/test/fixtures/esm/index.js @@ -0,0 +1 @@ +console.log('logged in fixtures/esm/index.js'); diff --git a/test/fixtures/esm/index.ts b/test/fixtures/esm/index.ts new file mode 100644 index 0000000..ed203aa --- /dev/null +++ b/test/fixtures/esm/index.ts @@ -0,0 +1 @@ +console.log('logged in fixtures/esm/index.ts'); diff --git a/test/fixtures/esm/tsconfig.json b/test/fixtures/esm/tsconfig.json new file mode 100644 index 0000000..41da438 --- /dev/null +++ b/test/fixtures/esm/tsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "outDir": "compiled" + }, + "include": [ + "." + ] +} diff --git a/test/fixtures/install-and-load.js b/test/fixtures/install-and-load.js index 5e1f26c..c92a633 100644 --- a/test/fixtures/install-and-load.js +++ b/test/fixtures/install-and-load.js @@ -1,19 +1,25 @@ -const path = require('path'); -const makeProvider = require('../..'); +import {createRequire} from 'node:module'; +import path from 'node:path'; +import process from 'node:process'; +import {fileURLToPath} from 'node:url'; +import makeProvider from '@ava/typescript'; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const provider = makeProvider({ negotiateProtocol() { return {identifier: 'ava-3.2', ava: {version: '3.15.0'}, projectDir: __dirname}; - } + }, }); const worker = provider.worker({ extensionsToLoadAsModules: [], - state: JSON.parse(process.argv[2]) + state: {}, + ...JSON.parse(process.argv[2]), }); const ref = path.resolve(process.argv[3]); if (worker.canLoad(ref)) { - worker.load(ref, {requireFn: require}); + worker.load(ref, {requireFn: createRequire(import.meta.url)}); } diff --git a/test/fixtures/typescript/package.json b/test/fixtures/typescript/package.json new file mode 100644 index 0000000..5bbefff --- /dev/null +++ b/test/fixtures/typescript/package.json @@ -0,0 +1,3 @@ +{ + "type": "commonjs" +} diff --git a/test/protocol-ava-3.2.js b/test/protocol-ava-3.2.js index 476895f..366ba1b 100644 --- a/test/protocol-ava-3.2.js +++ b/test/protocol-ava-3.2.js @@ -1,8 +1,11 @@ -const path = require('path'); -const test = require('ava'); -const pkg = require('../package.json'); -const createProviderMacro = require('./_with-provider'); - +import fs from 'node:fs'; +import path from 'node:path'; +import {fileURLToPath} from 'node:url'; +import test from 'ava'; +import createProviderMacro from './_with-provider.js'; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); +const pkg = fs.readFileSync(new URL('../package.json', import.meta.url)); const withProvider = createProviderMacro('ava-3.2', '3.15.0'); const validateConfig = (t, provider, config) => { @@ -81,6 +84,6 @@ test('main() updateGlobs()', withProvider, (t, provider) => { const main = provider.main({config: {rewritePaths: {'src/': 'build/'}, compile: false}}); t.snapshot(main.updateGlobs({ filePatterns: ['src/test.ts'], - ignoredByWatcherPatterns: ['assets/**'] + ignoredByWatcherPatterns: ['assets/**'], })); }); diff --git a/test/snapshots/compilation.js.snap b/test/snapshots/compilation.js.snap index 1d4c4ed..9171aa3 100644 Binary files a/test/snapshots/compilation.js.snap and b/test/snapshots/compilation.js.snap differ diff --git a/test/snapshots/esm.js.md b/test/snapshots/esm.js.md new file mode 100644 index 0000000..1642bed --- /dev/null +++ b/test/snapshots/esm.js.md @@ -0,0 +1,11 @@ +# Snapshot report for `test/esm.js` + +The actual snapshot is saved in `esm.js.snap`. + +Generated by [AVA](https://avajs.dev). + +## worker(): import ESM + +> Snapshot 1 + + 'logged in fixtures/esm/index.js' diff --git a/test/snapshots/esm.js.snap b/test/snapshots/esm.js.snap new file mode 100644 index 0000000..d31fee5 Binary files /dev/null and b/test/snapshots/esm.js.snap differ diff --git a/test/snapshots/protocol-ava-3.2.js.snap b/test/snapshots/protocol-ava-3.2.js.snap index 1e9982c..df5d750 100644 Binary files a/test/snapshots/protocol-ava-3.2.js.snap and b/test/snapshots/protocol-ava-3.2.js.snap differ