Skip to content

Commit

Permalink
Move to Jest
Browse files Browse the repository at this point in the history
- Remove mocha/chai/nyc dependencies
- Add jest + ts-jest dependencies
- Update eslintrc for jest
- Move mocha/nyc configs to jest config
- Add custom environment to fix Uint8Array issues
  (jestjs/jest#4422)
- Add custom reporters for cleaner output
- Update test files
- Update npm scripts
- Break up e2e tests into separate files for concurrency
  • Loading branch information
rkalis committed Jan 14, 2020
1 parent 30d6339 commit 8fdd9f2
Show file tree
Hide file tree
Showing 37 changed files with 3,702 additions and 16,921 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
sourceType: 'module', // Allows for the use of imports
},
env: {
'mocha': true,
'jest': true,
},
parserOptions: {
project: './tsconfig.build.json'
Expand Down
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
Expand Down
15 changes: 0 additions & 15 deletions .nycrc.json

This file was deleted.

3,830 changes: 0 additions & 3,830 deletions examples/yarn.lock

This file was deleted.

19 changes: 19 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: './jest/custom-environment',
verbose: false,
reporters: [
'./jest/log-on-fail-reporter.js',
'./jest/summary-reporter.js',
],
setupFilesAfterEnv: ['./jest.setup.js'],
collectCoverage: false,
collectCoverageFrom: [
'**/src/**/*.ts',
'!**/*.d.ts',
'!**/index.ts',
],
coveragePathIgnorePatterns: [
'<rootDir>/packages/cashc/src/grammar/',
],
};
1 change: 1 addition & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jest.setTimeout(25000);
32 changes: 32 additions & 0 deletions jest/custom-environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// my-custom-environment
const NodeEnvironment = require('jest-environment-node');

class CustomEnvironment extends NodeEnvironment {
constructor(config, context) {
Object.assign(
config.globals,
{
Uint8Array,
ArrayBuffer,
},
);
super(config, context);
this.testPath = context.testPath;
this.docblockPragmas = context.docblockPragmas;
}


async setup() {
await super.setup();
}

async teardown() {
await super.teardown();
}

runScript(script) {
return super.runScript(script);
}
}

module.exports = CustomEnvironment;
29 changes: 29 additions & 0 deletions jest/log-on-fail-reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const chalk = require('chalk');
const { getConsoleOutput } = require('jest-util');
const DefaultReporter = require('@jest/reporters/build/default_reporter').default;
const getResultHeader = require('@jest/reporters/build/get_result_header').default;

const TITLE_BULLET = chalk.bold('\u25cf ');

// This Jest reporter does not output any console.log except when the tests are
// failing, see: https://github.com/mozilla/addons-frontend/issues/2980.
class LogOnFailReporter extends DefaultReporter {
printTestFileHeader(testPath, config, result) {
this.log(getResultHeader(result, this._globalConfig, config));

const consoleBuffer = result.console;
const testFailed = result.numFailingTests > 0;

if (testFailed && consoleBuffer && consoleBuffer.length) {
this.log(
` ${TITLE_BULLET}Console\n\n${getConsoleOutput(
config.cwd,
!!this._globalConfig.verbose,
consoleBuffer,
)}`,
);
}
}
}

module.exports = LogOnFailReporter;
3 changes: 3 additions & 0 deletions jest/summary-reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { SummaryReporter } = require('@jest/reporters');

module.exports = SummaryReporter;
Loading

0 comments on commit 8fdd9f2

Please sign in to comment.