Skip to content

Commit

Permalink
convert all exports to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Dec 25, 2018
1 parent 511f5b2 commit ba200c6
Show file tree
Hide file tree
Showing 114 changed files with 441 additions and 508 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
### Chore & Maintenance

- `[*]` [**BREAKING**] Require Node.js 6+ for all packages ([#7258](https://github.com/facebook/jest/pull/7258))
- `[*]` [**BREAKING**] Use ESM `import` and `export` all over ([#7548](https://github.com/facebook/jest/pull/7548))
- `[jest-util]` [**BREAKING**] Remove long-deprecated globals for fake timers ([#7285](https://github.com/facebook/jest/pull/7285))
- `[docs]` Fix message property in custom matcher example to return a function instead of a constant. ([#7426](https://github.com/facebook/jest/pull/7426))
- `[jest-circus]` Standardize file naming in `jest-circus` ([#7301](https://github.com/facebook/jest/pull/7301))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ exports[`moduleNameMapper wrong configuration 1`] = `
12 | module.exports = () => 'test';
13 |
at packages/jest-resolve/build/index.js:433:17
at packages/jest-resolve/build/index.js:452:17
at index.js:10:1
"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ exports[`show error message with matching files 1`] = `
| ^
4 |
at packages/jest-resolve/build/index.js:200:17
at packages/jest-resolve/build/index.js:217:17
at index.js:3:18
"
Expand Down
4 changes: 2 additions & 2 deletions e2e/browser-support/browser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/

/* eslint-disable */
var expect = require('../../packages/expect/build-es5/index.js');
var {default: expect} = require('../../packages/expect/build-es5/index.js');
var mock = require('../../packages/jest-mock/build-es5/index.js');
var prettyFormat = require('../../packages/pretty-format/build-es5/index.js');
var {default: prettyFormat} = require('../../packages/pretty-format/build-es5/index.js');

describe('es5 builds in browser', function() {
it('runs assertions', function() {
Expand Down
2 changes: 1 addition & 1 deletion e2e/runJest.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import path from 'path';
import fs from 'fs';
import execa, {sync as spawnSync} from 'execa';
import {Writable} from 'readable-stream';
const stripAnsi = require('strip-ansi');
import stripAnsi from 'strip-ansi';
import {normalizeIcons} from './Utils';

const JEST_PATH = path.resolve(__dirname, '../packages/jest-cli/bin/jest.js');
Expand Down
2 changes: 1 addition & 1 deletion e2e/test-environment-async/TestEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
const fs = require('fs');
const os = require('os');
const mkdirp = require('mkdirp');
const JSDOMEnvironment = require('jest-environment-jsdom');
const JSDOMEnvironment = require('jest-environment-jsdom').default;

const DIR = os.tmpdir() + '/jest-test-environment';

Expand Down
3 changes: 2 additions & 1 deletion packages/babel-jest/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const babelJest = require('../index');

import babelJest from '../index';

//Mock data for all the tests
const sourceString = `
Expand Down
5 changes: 2 additions & 3 deletions packages/babel-jest/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import babelIstanbulPlugin from 'babel-plugin-istanbul';
const THIS_FILE = fs.readFileSync(__filename);
const jestPresetPath = require.resolve('babel-preset-jest');

const createTransformer = (options: any): Transformer => {
export const createTransformer = (options: any): Transformer => {
options = Object.assign({}, options, {
compact: false,
plugins: (options && options.plugins) || [],
Expand Down Expand Up @@ -110,5 +110,4 @@ const createTransformer = (options: any): Transformer => {
};
};

module.exports = createTransformer();
(module.exports: any).createTransformer = createTransformer;
export default createTransformer();
2 changes: 1 addition & 1 deletion packages/babel-plugin-jest-hoist/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ FUNCTIONS.deepUnmock = args => args.length === 1 && args[0].isStringLiteral();
FUNCTIONS.disableAutomock = FUNCTIONS.enableAutomock = args =>
args.length === 0;

module.exports = () => {
export default () => {
const isJest = callee =>
callee.get('object').isIdentifier(JEST_GLOBAL) ||
(callee.isMemberExpression() && isJest(callee.get('object')));
Expand Down
14 changes: 7 additions & 7 deletions packages/expect/src/__tests__/assertionCounts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,35 @@

'use strict';

const jestExpect = require('../');
import jestExpect, {assertions, hasAssertions} from '../';

describe('.assertions()', () => {
it('does not throw', () => {
jestExpect.assertions(2);
assertions(2);
jestExpect('a').not.toBe('b');
jestExpect('a').toBe('a');
});

it('redeclares different assertion count', () => {
jestExpect.assertions(3);
assertions(3);
jestExpect('a').not.toBe('b');
jestExpect('a').toBe('a');
jestExpect.assertions(2);
assertions(2);
});
it('expects no assertions', () => {
jestExpect.assertions(0);
assertions(0);
});
});

describe('.hasAssertions()', () => {
it('does not throw if there is an assertion', () => {
jestExpect.hasAssertions();
hasAssertions();
jestExpect('a').toBe('a');
});

it('throws if expected is not undefined', () => {
jestExpect(() => {
jestExpect.hasAssertions(2);
hasAssertions(2);
}).toThrowErrorMatchingSnapshot();
});

Expand Down
8 changes: 4 additions & 4 deletions packages/expect/src/__tests__/asymmetricMatchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@

'use strict';

const jestExpect = require('../');
const {
import jestExpect from '../';
import {
any,
anything,
arrayContaining,
arrayNotContaining,
objectContaining,
objectNotContaining,
stringContaining,
stringNotContaining,
stringMatching,
stringNotContaining,
stringNotMatching,
} = require('../asymmetricMatchers');
} from '../asymmetricMatchers';

test('Any.asymmetricMatch()', () => {
const Thing = function() {};
Expand Down
8 changes: 4 additions & 4 deletions packages/expect/src/__tests__/extend.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
*
*/

const matcherUtils = require('jest-matcher-utils');
const {iterableEquality, subsetEquality} = require('../utils');
const {equals} = require('../jasmineUtils');
const jestExpect = require('../');
import * as matcherUtils from 'jest-matcher-utils';
import {iterableEquality, subsetEquality} from '../utils';
import {equals} from '../jasmineUtils';
import jestExpect from '../';

jestExpect.extend({
toBeDivisibleBy(actual, expected) {
Expand Down
9 changes: 5 additions & 4 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
*
*/

const {stringify} = require('jest-matcher-utils');
const jestExpect = require('../');
const Immutable = require('immutable');
const chalk = require('chalk');
import {stringify} from 'jest-matcher-utils';
import jestExpect from '../';
import Immutable from 'immutable';
import chalk from 'chalk';

const chalkEnabled = chalk.enabled;

beforeAll(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/expect/src/__tests__/spyMatchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*
*/

const Immutable = require('immutable');
const jestExpect = require('../');
import Immutable from 'immutable';
import jestExpect from '../';

['toBeCalled', 'toHaveBeenCalled'].forEach(called => {
describe(`${called}`, () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/expect/src/__tests__/stacktrace.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

const jestExpect = require('../');
import jestExpect from '../';

jestExpect.extend({
toCustomMatch(callback, expectation) {
Expand Down
2 changes: 1 addition & 1 deletion packages/expect/src/__tests__/toThrowMatchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

'use strict';

const jestExpect = require('../');
import jestExpect from '../';

// Custom Error class because node versions have different stack trace strings.
class customError extends Error {
Expand Down
6 changes: 3 additions & 3 deletions packages/expect/src/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

'use strict';

const {stringify} = require('jest-matcher-utils');
const {
import {stringify} from 'jest-matcher-utils';
import {
emptyObject,
getObjectSubset,
getPath,
hasOwnProperty,
subsetEquality,
} = require('../utils');
} from '../utils';

describe('getPath()', () => {
test('property exists', () => {
Expand Down
27 changes: 14 additions & 13 deletions packages/expect/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

import type {
Expect,
ExpectationObject,
AsyncExpectationResult,
SyncExpectationResult,
Expand Down Expand Up @@ -47,7 +46,12 @@ import {
getMatchers,
setMatchers,
} from './jestMatchersObject';
import extractExpectedAssertionsErrors from './extractExpectedAssertionsErrors';

export {
default as extractExpectedAssertionsErrors,
} from './extractExpectedAssertionsErrors';

export {getState, setState};

class JestAssertionError extends Error {
matcherResult: any;
Expand Down Expand Up @@ -77,7 +81,7 @@ const getPromiseMatcher = (name, matcher) => {
return null;
};

const expect = (actual: any, ...rest): ExpectationObject => {
const expect = (actual: any, ...rest: Array<any>): ExpectationObject => {
if (rest.length !== 0) {
throw new Error('Expect takes at most one argument.');
}
Expand Down Expand Up @@ -316,9 +320,11 @@ const makeThrowingMatcher = (
}
};

expect.extend = (matchers: MatchersObject): void =>
export const extend = (matchers: MatchersObject): void =>
setMatchers(matchers, false, expect);

expect.extend = extend;

expect.anything = anything;
expect.any = any;

Expand Down Expand Up @@ -352,7 +358,7 @@ const _validateResult = result => {
}
};

function assertions(expected: number) {
export function assertions(expected: number) {
const error = new Error();
if (Error.captureStackTrace) {
Error.captureStackTrace(error, assertions);
Expand All @@ -361,7 +367,7 @@ function assertions(expected: number) {
getState().expectedAssertionsNumber = expected;
getState().expectedAssertionsNumberError = error;
}
function hasAssertions(...args) {
export function hasAssertions(...args: Array<any>) {
const error = new Error();
if (Error.captureStackTrace) {
Error.captureStackTrace(error, hasAssertions);
Expand All @@ -377,11 +383,6 @@ setMatchers(matchers, true, expect);
setMatchers(spyMatchers, true, expect);
setMatchers(toThrowMatchers, true, expect);

expect.addSnapshotSerializer = () => void 0;
expect.assertions = assertions;
expect.hasAssertions = hasAssertions;
expect.getState = getState;
expect.setState = setState;
expect.extractExpectedAssertionsErrors = extractExpectedAssertionsErrors;
export const addSnapshotSerializer = () => void 0;

module.exports = (expect: Expect);
export default expect;
8 changes: 2 additions & 6 deletions packages/expect/src/jestMatchersObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
*/

import {AsymmetricMatcher} from './asymmetricMatchers';
import type {
Expect,
MatchersObject,
SyncExpectationResult,
} from 'types/Matchers';
import type {MatchersObject, SyncExpectationResult} from 'types/Matchers';

// Global matchers object holds the list of available matchers and
// the state, that can hold matcher specific values that change over time.
Expand Down Expand Up @@ -47,7 +43,7 @@ export const getMatchers = () => global[JEST_MATCHERS_OBJECT].matchers;
export const setMatchers = (
matchers: MatchersObject,
isInternal: boolean,
expect: Expect,
expect: any,
) => {
Object.keys(matchers).forEach(key => {
const matcher = matchers[key];
Expand Down
12 changes: 1 addition & 11 deletions packages/jest-circus/src/__tests__/circusItTestError.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,9 @@

'use strict';

let circusIt;
let circusTest;

// using jest-jasmine2's 'it' to test jest-circus's 'it'. Had to differentiate
// the two with this alias.

const aliasCircusIt = () => {
const {it, test} = require('../index.js');
circusIt = it;
circusTest = test;
};

aliasCircusIt();
import {it as circusIt, test as circusTest} from '../index.js';

// A few of these tests require incorrect types to throw errors and thus pass
// the test. The typechecks on jest-circus would prevent that, so
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,9 @@

'use strict';

let circusIt;

// using jest-jasmine2's 'it' to test jest-circus's 'it'. Had to differentiate
// the two with this alias.

const aliasCircusIt = () => {
const {it} = require('../index.js');
circusIt = it;
};

aliasCircusIt();
import {it as circusIt} from '../index.js';

describe('test/it.todo error throwing', () => {
it('todo throws error when given no arguments', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-circus/src/__tests__/hooksError.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

'use strict';

const circus = require('../index.js');
import * as circus from '../index.js';

describe.each([['beforeEach'], ['beforeAll'], ['afterEach'], ['afterAll']])(
'%s hooks error throwing',
Expand All @@ -27,6 +27,7 @@ describe.each([['beforeEach'], ['beforeAll'], ['afterEach'], ['afterAll']])(
`${fn} throws an error when %p is provided as a first argument to it`,
el => {
expect(() => {
// eslint-disable-next-line import/namespace
circus[fn](el);
}).toThrowError(
'Invalid first argument. It must be a callback function.',
Expand Down
Loading

0 comments on commit ba200c6

Please sign in to comment.