Skip to content

Commit

Permalink
fixup! fixup! fixup! fixup! fixup! fixup! fixup! Convert to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
novemberborn committed May 14, 2021
1 parent d2cf00e commit b32ff7a
Show file tree
Hide file tree
Showing 32 changed files with 108 additions and 76 deletions.
1 change: 1 addition & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ export default async () => { // eslint-disable-line complexity
reportStream: process.stdout,
stdStream: process.stderr
}) : new DefaultReporter({
extensions: globs.extensions,
projectDir,
reportStream: process.stdout,
stdStream: process.stderr,
Expand Down
4 changes: 3 additions & 1 deletion lib/reporters/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ function manageCorking(stream) {

export default class Reporter {
constructor({
extensions,
verbose,
reportStream,
stdStream,
Expand All @@ -149,6 +150,7 @@ export default class Reporter {
spinner,
durationThreshold
}) {
this.extensions = extensions;
this.verbose = verbose;
this.reportStream = reportStream;
this.stdStream = stdStream;
Expand Down Expand Up @@ -223,7 +225,7 @@ export default class Reporter {
this.emptyParallelRun = plan.status.emptyParallelRun;

if (this.watching || plan.files.length > 1) {
this.prefixTitle = (testFile, title) => prefixTitle(plan.filePathPrefix, testFile, title);
this.prefixTitle = (testFile, title) => prefixTitle(this.extensions, plan.filePathPrefix, testFile, title);
}

this.removePreviousListener = plan.status.on('stateChange', evt => {
Expand Down
4 changes: 2 additions & 2 deletions lib/reporters/prefix-title.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {chalk} from '../chalk.js';

const SEPERATOR = ' ' + chalk.gray.dim(figures.pointerSmall) + ' ';

export default (base, file, title) => {
export default (extensions, base, file, title) => {
const prefix = file
// Only replace base if it is found at the start of the path
.replace(base, (match, offset) => offset === 0 ? '' : match)
.replace(/\.spec/, '')
.replace(/\.test/, '')
.replace(/test-/g, '')
.replace(/\.js$/, '')
.replace(new RegExp(`.(${extensions.join('|')})$`), '')
.split(path.sep)
.filter(p => p !== '__tests__')
.join(SEPERATOR);
Expand Down
12 changes: 10 additions & 2 deletions lib/watcher.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nodePath from 'path';

import chokidar from 'chokidar';
import chokidar_ from 'chokidar';
import createDebug from 'debug';
import diff from 'lodash/difference.js';
import flatten from 'lodash/flatten.js';
Expand All @@ -9,7 +9,15 @@ import {chalk} from './chalk.js';
import {applyTestFileFilter, classify, getChokidarIgnorePatterns} from './globs.js';
import providerManager from './provider-manager.js';

const debug = createDebug('ava:watcher');
let chokidar = chokidar_;
export function _injectChokidar(injected) {
chokidar = injected;
}

let debug = createDebug('ava:watcher');
export function _injectDebug(injected) {
debug = injected('ava:watcher');
}

function rethrowAsync(error) {
// Don't swallow exceptions. Note that any
Expand Down
10 changes: 5 additions & 5 deletions test-tap/fixture/_generate-source-map-initial.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ test('throw an uncaught exception', t => {
})
const run = () => fixture.run();
`.trim(), {
filename: 'source-map-initial-input.js',
filename: 'source-map-initial-input.cjs',
sourceMaps: true,
presets: ['@ava/stage-4']
});

fs.writeFileSync(
path.join(__dirname, 'source-map-initial.js'),
transformed.code + '\n//# sourceMappingURL=./source-map-initial.js.map\n// Generated using node test/fixtures/_generate-source-map-initial.js\n'
path.join(__dirname, 'source-map-initial.cjs'),
transformed.code + '\n//# sourceMappingURL=./source-map-initial.cjs.map\n// Generated using node test/fixtures/_generate-source-map-initial.js\n'
);
fs.writeFileSync(
path.join(__dirname, 'source-map-initial.js.map'),
path.join(__dirname, 'source-map-initial.cjs.map'),
JSON.stringify(transformed.map)
);

console.log('Generated source-map-initial.js');
console.log('Generated source-map-initial.cjs');
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"ava": {
"files": ["*.js"]
"files": [
"*.cjs"
]
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"ava": {
"files": ["*.js"]
"files": [
"*.cjs"
]
}
}
4 changes: 3 additions & 1 deletion test-tap/fixture/parallel-runs/no-files/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"ava": {
"files": ["*.js"]
"files": [
"*.cjs"
]
}
}
File renamed without changes.
4 changes: 3 additions & 1 deletion test-tap/fixture/snapshots/test-content/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"ava": {
"files": ["tests/test.js"]
"files": [
"tests/test.cjs"
]
}
}
2 changes: 1 addition & 1 deletion test-tap/fixture/snapshots/test-dir/test/test.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from '../../../../../entrypoints/main.cjs';
const test = require('../../../../../entrypoints/main.cjs');

test('test title', t => {
t.snapshot({foo: 'bar'});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"ava": {
"files": [
"src/**/*test.js"
"src/**/*test.cjs"
],
"snapshotDir": "snapshot-fixtures"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ test('feature test title', t => {
test('another feature test', t => {
t.snapshot(new Map());
});
//# sourceMappingURL=test.js.map
//# sourceMappingURL=test.cjs.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"version": 3,
"file": "test.cjs",
"sourceRoot": "",
"sources": [
"../../../src/feature/__tests__/test.ts"
],
"names": [],
"mappings": "AAAA,OAAO,IAAI,MAAM,sBAAsB,CAAA;AAEvC,IAAI,CAAC,oBAAoB,EAAE,CAAC;IAC3B,CAAC,CAAC,QAAQ,CAAC,EAAC,GAAG,EAAE,KAAK,EAAC,CAAC,CAAC;IAEzB,CAAC,CAAC,QAAQ,CAAC,EAAC,MAAM,EAAE,EAAE,EAAC,CAAC,CAAC;AAC1B,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,sBAAsB,EAAE,CAAC;IAC7B,CAAC,CAAC,QAAQ,CAAC,IAAI,GAAG,EAAE,CAAC,CAAC;AACvB,CAAC,CAAC,CAAC"
}

This file was deleted.

2 changes: 1 addition & 1 deletion test-tap/fixture/snapshots/test-sourcemaps/build/test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ test('top level test title', t => {
test('another top level test', t => {
t.snapshot(new Map());
});
//# sourceMappingURL=test.js.map
//# sourceMappingURL=test.cjs.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import test from '../../../../../../entrypoints/main.cjs';
const test = require('../../../../../../entrypoints/main.cjs');
test('test title', t => {
t.snapshot({ foo: 'bar' });
t.snapshot({ answer: 43 });
});
test('another test', t => {
t.snapshot(new Map());
});
//# sourceMappingURL=test.js.map
//# sourceMappingURL=test.cjs.map
10 changes: 10 additions & 0 deletions test-tap/fixture/snapshots/test-sourcemaps/build/test/test.cjs.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"version": 3,
"file": "test.cjs",
"sourceRoot": "",
"sources": [
"../../src/test/test.ts"
],
"names": [],
"mappings": "AAAA,OAAO,IAAI,MAAM,mBAAmB,CAAC;AAErC,IAAI,CAAC,YAAY,EAAE,CAAC;IACnB,CAAC,CAAC,QAAQ,CAAC,EAAC,GAAG,EAAE,KAAK,EAAC,CAAC,CAAC;IAEzB,CAAC,CAAC,QAAQ,CAAC,EAAC,MAAM,EAAE,EAAE,EAAC,CAAC,CAAC;AAC1B,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,cAAc,EAAE,CAAC;IACrB,CAAC,CAAC,QAAQ,CAAC,IAAI,GAAG,EAAE,CAAC,CAAC;AACvB,CAAC,CAAC,CAAC"
}

This file was deleted.

2 changes: 1 addition & 1 deletion test-tap/fixture/snapshots/test-sourcemaps/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"ava": {
"files": [
"build/**/test.js"
"build/**/test.cjs"
]
}
}
4 changes: 3 additions & 1 deletion test-tap/fixture/snapshots/tests-dir/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"ava": {
"files": ["tests/**/*.js"]
"files": [
"tests/**/*.cjs"
]
}
}
4 changes: 2 additions & 2 deletions test-tap/fixture/source-map-initial.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ const fixture = (0, _sourceMapFixtures.mapFile)('throws').require();
t.pass();
});
const run = () => fixture.run();
// # sourceMappingURL=./source-map-initial.js.map
// Generated using node test/fixtures/_generate-source-map-initial.js
// # sourceMappingURL=./source-map-initial.cjs.map
// Generated using node test/fixtures/_generate-source-map-initial.cjs
2 changes: 1 addition & 1 deletion test-tap/fixture/source-map-initial.cjs.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions test-tap/helper/report.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import childProcess from 'child_process';
import fs from 'fs';
import {createRequire} from 'module';
import path from 'path';
import {fileURLToPath} from 'url';
import workerThreads from 'worker_threads';

import globby from 'globby';
import proxyquire from 'proxyquire';
import replaceString from 'replace-string';

import {normalizeGlobs} from '../../lib/globs.js';
Expand All @@ -27,8 +27,10 @@ class Worker extends workerThreads.Worker {
}

let _Api = null;
const createApi = options => {
const createApi = async options => {
if (!_Api) {
const require = createRequire(import.meta.url);
const proxyquire = require('proxyquire');
_Api = proxyquire('../../lib/api', {
'./fork': proxyquire('../../lib/fork', {
worker_threads: { // eslint-disable-line camelcase
Expand Down Expand Up @@ -93,13 +95,13 @@ exports.sanitizers = {
const __dirname = fileURLToPath(new URL('.', import.meta.url));
exports.projectDir = type => path.join(__dirname, '../fixture/report', type.toLowerCase());

const run = (type, reporter, {match = [], filter} = {}) => {
const run = async (type, reporter, {match = [], filter} = {}) => {
const projectDir = exports.projectDir(type);

const providers = [{
type: 'babel',
level: 'ava-3',
main: providerManager.babel(projectDir).main({
main: (await providerManager.babel(projectDir)).main({
config: {
testOptions: {
plugins: ['@babel/plugin-proposal-do-expressions']
Expand Down Expand Up @@ -128,7 +130,7 @@ const run = (type, reporter, {match = [], filter} = {}) => {

options.globs = normalizeGlobs({extensions: options.extensions, files: ['*'], providers: []});

const api = createApi(options);
const api = await createApi(options);
api.on('run', plan => reporter.startRun(plan));

const files = globby.sync('*.cjs', {
Expand Down
4 changes: 2 additions & 2 deletions test-tap/integration/assorted.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ test('timeout', t => {
test('include anonymous functions in error reports', t => {
execCli('error-in-anonymous-function.cjs', (err, stdout) => {
t.ok(err);
t.match(stdout, /error-in-anonymous-function\.js:4:8/);
t.match(stdout, /error-in-anonymous-function\.cjs:4:8/);
t.end();
});
});
Expand Down Expand Up @@ -146,7 +146,7 @@ test('load .mjs test files', t => {
});

test('load .js test files as ESM modules', t => {
execCli('test.cjs', {dirname: 'fixture/pkg-type-module'}, (err, stdout) => {
execCli('test.js', {dirname: 'fixture/pkg-type-module'}, (err, stdout) => {
t.error(err);
t.match(stdout, /1 test passed/);
t.end();
Expand Down
20 changes: 10 additions & 10 deletions test-tap/integration/snapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ for (const object of [
{type: 'tests', rel: 'tests-dir', dir: 'tests/snapshots'}
]) {
test(`snapshots work (${object.type})`, t => {
const snapPath = path.join(__dirname, '..', 'fixture', 'snapshots', object.rel, object.dir, 'test.js.snap');
const snapPath = path.join(__dirname, '..', 'fixture', 'snapshots', object.rel, object.dir, 'test.cjs.snap');
try {
fs.unlinkSync(snapPath);
} catch (error) {
Expand Down Expand Up @@ -78,7 +78,7 @@ test('two', t => {
});

test('outdated snapshot version is reported to the console', t => {
const snapPath = path.join(__dirname, '..', 'fixture', 'snapshots', 'test.js.snap');
const snapPath = path.join(__dirname, '..', 'fixture', 'snapshots', 'test.cjs.snap');
fs.writeFileSync(snapPath, Buffer.from([0x0A, 0x00, 0x00]));

execCli(['test.cjs'], {dirname: 'fixture/snapshots'}, (error, stdout) => {
Expand All @@ -92,7 +92,7 @@ test('outdated snapshot version is reported to the console', t => {
});

test('outdated snapshot version can be updated', t => {
const snapPath = path.join(__dirname, '..', 'fixture', 'snapshots', 'test.js.snap');
const snapPath = path.join(__dirname, '..', 'fixture', 'snapshots', 'test.cjs.snap');
fs.writeFileSync(snapPath, Buffer.from([0x0A, 0x00, 0x00]));

execCli(['test.cjs', '--update-snapshots'], {dirname: 'fixture/snapshots', env: {AVA_FORCE_CI: 'not-ci'}}, (error, stdout) => {
Expand All @@ -103,7 +103,7 @@ test('outdated snapshot version can be updated', t => {
});

test('newer snapshot version is reported to the console', t => {
const snapPath = path.join(__dirname, '..', 'fixture', 'snapshots', 'test.js.snap');
const snapPath = path.join(__dirname, '..', 'fixture', 'snapshots', 'test.cjs.snap');
fs.writeFileSync(snapPath, Buffer.from([0x0A, 0xFF, 0xFF]));

execCli(['test.cjs'], {dirname: 'fixture/snapshots'}, (error, stdout) => {
Expand All @@ -117,7 +117,7 @@ test('newer snapshot version is reported to the console', t => {
});

test('snapshot corruption is reported to the console', t => {
const snapPath = path.join(__dirname, '..', 'fixture', 'snapshots', 'test.js.snap');
const snapPath = path.join(__dirname, '..', 'fixture', 'snapshots', 'test.cjs.snap');
fs.writeFileSync(snapPath, Buffer.from([0x0A, 0x03, 0x00]));

execCli(['test.cjs'], {dirname: 'fixture/snapshots'}, (error, stdout) => {
Expand All @@ -131,7 +131,7 @@ test('snapshot corruption is reported to the console', t => {
});

test('legacy snapshot files are reported to the console', t => {
const snapPath = path.join(__dirname, '..', 'fixture', 'snapshots', 'test.js.snap');
const snapPath = path.join(__dirname, '..', 'fixture', 'snapshots', 'test.cjs.snap');
fs.writeFileSync(snapPath, Buffer.from('// Jest Snapshot v1, https://goo.gl/fbAQLP\n'));

execCli(['test.cjs'], {dirname: 'fixture/snapshots'}, (error, stdout) => {
Expand Down Expand Up @@ -203,8 +203,8 @@ test('snapshots resolved location from "snapshotDir" in AVA config', t => {
.map(snapRelativeDir => {
const snapPath = path.join(__dirname, '..', relativeFixtureDir, snapDir, snapRelativeDir);
return [
path.join(snapPath, 'test.js.md'),
path.join(snapPath, 'test.js.snap')
path.join(snapPath, 'test.cjs.md'),
path.join(snapPath, 'test.cjs.snap')
];
})
.reduce((a, b) => [...a, ...b], []);
Expand Down Expand Up @@ -278,8 +278,8 @@ test('snapshots are identical on different platforms', t => {

test('in CI, new snapshots are not recorded', t => {
const fixtureDir = path.join(__dirname, '..', 'fixture', 'snapshots', 'test-content');
const reportPath = path.join(fixtureDir, 'tests', 'snapshots', 'test.js.md');
const snapPath = path.join(fixtureDir, 'tests', 'snapshots', 'test.js.snap');
const reportPath = path.join(fixtureDir, 'tests', 'snapshots', 'test.cjs.md');
const snapPath = path.join(fixtureDir, 'tests', 'snapshots', 'test.cjs.snap');

const removeFile = filePath => {
try {
Expand Down
8 changes: 4 additions & 4 deletions test-tap/integration/stack-traces.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {execCli} from '../helper/cli.js';
test('`AssertionError` should capture infinity stack trace', t => {
execCli('infinity-stack-trace.cjs', (err, stdout) => {
t.ok(err);
t.match(stdout, /c \(.*infinity-stack-trace\.js:6:20\)/);
t.match(stdout, /b \(.*infinity-stack-trace\.js:7:18\)/);
t.match(stdout, /a \(.*infinity-stack-trace\.js:8:18\)/);
t.match(stdout, /.+?infinity-stack-trace\.js:10:2/);
t.match(stdout, /c \(.*infinity-stack-trace\.cjs:6:20\)/);
t.match(stdout, /b \(.*infinity-stack-trace\.cjs:7:18\)/);
t.match(stdout, /a \(.*infinity-stack-trace\.cjs:8:18\)/);
t.match(stdout, /.+?infinity-stack-trace\.cjs:10:2/);
t.end();
});
});

0 comments on commit b32ff7a

Please sign in to comment.