Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
piranna committed Feb 7, 2022
2 parents f3304c4 + a9b7e8a commit ef89ae8
Show file tree
Hide file tree
Showing 93 changed files with 318 additions and 201 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
bin/
flow-typed/**
packages/*/build/**
packages/*/dist/**
packages/jest-diff/src/cleanupSemantic.ts
website/.docusaurus
website/blog
Expand Down
19 changes: 13 additions & 6 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@
* LICENSE file in the root directory of this source tree.
*/

const {sync: readPkg} = require('read-pkg');
const {getPackages} = require('./scripts/buildUtils');

const internalPackages = getPackages()
.map(packageDir => {
const pkg = readPkg({cwd: packageDir});

return pkg.name;
})
.map(({pkg}) => pkg.name)
.sort();

module.exports = {
Expand Down Expand Up @@ -185,6 +180,18 @@ module.exports = {
files: 'packages/**/*.ts',
rules: {
'@typescript-eslint/explicit-module-boundary-types': 'error',
'import/no-anonymous-default-export': [
'error',
{
allowAnonymousClass: false,
allowAnonymousFunction: false,
allowArray: false,
allowArrowFunction: false,
allowCallExpression: false,
allowLiteral: false,
allowObject: true,
},
],
},
},
{
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/node_modules

/packages/*/build/
/packages/*/dist/
/packages/*/coverage/
/packages/*/node_modules/

Expand All @@ -40,6 +41,7 @@ junit.xml
package-lock.json

*.tsbuildinfo
api-extractor.json

# https://yarnpkg.com/getting-started/qa#which-files-should-be-gitignored but nested for e2e directories
**/.yarn/*
Expand Down
5 changes: 0 additions & 5 deletions .npmignore

This file was deleted.

6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

- `[jest-environment-jsdom]` [**BREAKING**] Add default `browser` condtion to `exportConditions` for `jsdom` environment ([#11924](https://github.com/facebook/jest/pull/11924))
- `[jest-environment-node]` [**BREAKING**] Add default `node` and `node-addon` conditions to `exportConditions` for `node` environment ([#11924](https://github.com/facebook/jest/pull/11924))
- `[pretty-format]` Expose `ConvertAnsi` plugin ([#12308](https://github.com/facebook/jest/pull/12308))

### Fixes

- `[expect]` Add type definitions for asymmetric `closeTo` matcher ([#12304](https://github.com/facebook/jest/pull/12304))
- `[jest-repl]` Make module importable ([#12311](https://github.com/facebook/jest/pull/12311))

### Chore & Maintenance

- `[*]` Avoid anonymous default exports ([#12313](https://github.com/facebook/jest/pull/12313))

### Performance

## 27.5.0
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = {
escapeString: false,
},
snapshotSerializers: [
// change to require.resolve('pretty-format/ConvertAnsi') when we drop Node 10
'<rootDir>/packages/pretty-format/build/plugins/ConvertAnsi.js',
],
testPathIgnorePatterns: [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"which": "^2.0.1"
},
"scripts": {
"build-clean": "rimraf './packages/*/build' './packages/*/tsconfig.tsbuildinfo'",
"build-clean": "rimraf './packages/*/build' './packages/*/dist' './packages/*/tsconfig.tsbuildinfo'",
"build": "yarn build:js && yarn build:ts",
"build:js": "node ./scripts/build.js",
"build:ts": "node ./scripts/buildTs.js",
Expand Down
1 change: 1 addition & 0 deletions packages/babel-jest/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
src
tsconfig.json
tsconfig.tsbuildinfo
api-extractor.json
1 change: 1 addition & 0 deletions packages/babel-plugin-jest-hoist/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
src
tsconfig.json
tsconfig.tsbuildinfo
api-extractor.json
172 changes: 88 additions & 84 deletions packages/babel-plugin-jest-hoist/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,102 +268,106 @@ const extractJestObjExprIfHoistable = <T extends Node>(
};

/* eslint-disable sort-keys */
export default (): PluginObj<{
export default function jestHoist(): PluginObj<{
declareJestObjGetterIdentifier: () => Identifier;
jestObjGetterIdentifier?: Identifier;
}> => ({
pre({path: program}) {
this.declareJestObjGetterIdentifier = () => {
if (this.jestObjGetterIdentifier) {
return this.jestObjGetterIdentifier;
}
}> {
return {
pre({path: program}) {
this.declareJestObjGetterIdentifier = () => {
if (this.jestObjGetterIdentifier) {
return this.jestObjGetterIdentifier;
}

this.jestObjGetterIdentifier =
program.scope.generateUidIdentifier('getJestObj');
this.jestObjGetterIdentifier =
program.scope.generateUidIdentifier('getJestObj');

program.unshiftContainer('body', [
createJestObjectGetter({
GETTER_NAME: this.jestObjGetterIdentifier.name,
JEST_GLOBALS_MODULE_JEST_EXPORT_NAME,
JEST_GLOBALS_MODULE_NAME,
}),
]);
program.unshiftContainer('body', [
createJestObjectGetter({
GETTER_NAME: this.jestObjGetterIdentifier.name,
JEST_GLOBALS_MODULE_JEST_EXPORT_NAME,
JEST_GLOBALS_MODULE_NAME,
}),
]);

return this.jestObjGetterIdentifier;
};
},
visitor: {
ExpressionStatement(exprStmt) {
const jestObjExpr = extractJestObjExprIfHoistable(
exprStmt.get('expression'),
);
if (jestObjExpr) {
jestObjExpr.replaceWith(
callExpression(this.declareJestObjGetterIdentifier(), []),
return this.jestObjGetterIdentifier;
};
},
visitor: {
ExpressionStatement(exprStmt) {
const jestObjExpr = extractJestObjExprIfHoistable(
exprStmt.get('expression'),
);
}
if (jestObjExpr) {
jestObjExpr.replaceWith(
callExpression(this.declareJestObjGetterIdentifier(), []),
);
}
},
},
},
// in `post` to make sure we come after an import transform and can unshift above the `require`s
post({path: program}) {
const self = this;

visitBlock(program);
program.traverse({BlockStatement: visitBlock});

function visitBlock(block: NodePath<BlockStatement> | NodePath<Program>) {
// use a temporary empty statement instead of the real first statement, which may itself be hoisted
const [varsHoistPoint, callsHoistPoint] = block.unshiftContainer('body', [
emptyStatement(),
emptyStatement(),
]);
block.traverse({
CallExpression: visitCallExpr,
VariableDeclarator: visitVariableDeclarator,
// do not traverse into nested blocks, or we'll hoist calls in there out to this block
blacklist: ['BlockStatement'],
});
callsHoistPoint.remove();
varsHoistPoint.remove();

function visitCallExpr(callExpr: NodePath<CallExpression>) {
const {
node: {callee},
} = callExpr;
if (
isIdentifier(callee) &&
callee.name === self.jestObjGetterIdentifier?.name
) {
const mockStmt = callExpr.getStatementParent();

if (mockStmt) {
const mockStmtParent = mockStmt.parentPath;
if (mockStmtParent.isBlock()) {
const mockStmtNode = mockStmt.node;
mockStmt.remove();
callsHoistPoint.insertBefore(mockStmtNode);
// in `post` to make sure we come after an import transform and can unshift above the `require`s
post({path: program}) {
const self = this;

visitBlock(program);
program.traverse({BlockStatement: visitBlock});

function visitBlock(block: NodePath<BlockStatement> | NodePath<Program>) {
// use a temporary empty statement instead of the real first statement, which may itself be hoisted
const [varsHoistPoint, callsHoistPoint] = block.unshiftContainer(
'body',
[emptyStatement(), emptyStatement()],
);
block.traverse({
CallExpression: visitCallExpr,
VariableDeclarator: visitVariableDeclarator,
// do not traverse into nested blocks, or we'll hoist calls in there out to this block
blacklist: ['BlockStatement'],
});
callsHoistPoint.remove();
varsHoistPoint.remove();

function visitCallExpr(callExpr: NodePath<CallExpression>) {
const {
node: {callee},
} = callExpr;
if (
isIdentifier(callee) &&
callee.name === self.jestObjGetterIdentifier?.name
) {
const mockStmt = callExpr.getStatementParent();

if (mockStmt) {
const mockStmtParent = mockStmt.parentPath;
if (mockStmtParent.isBlock()) {
const mockStmtNode = mockStmt.node;
mockStmt.remove();
callsHoistPoint.insertBefore(mockStmtNode);
}
}
}
}
}

function visitVariableDeclarator(varDecl: NodePath<VariableDeclarator>) {
if (hoistedVariables.has(varDecl.node)) {
// should be assert function, but it's not. So let's cast below
varDecl.parentPath.assertVariableDeclaration();

const {kind, declarations} = varDecl.parent as VariableDeclaration;
if (declarations.length === 1) {
varDecl.parentPath.remove();
} else {
varDecl.remove();
function visitVariableDeclarator(
varDecl: NodePath<VariableDeclarator>,
) {
if (hoistedVariables.has(varDecl.node)) {
// should be assert function, but it's not. So let's cast below
varDecl.parentPath.assertVariableDeclaration();

const {kind, declarations} = varDecl.parent as VariableDeclaration;
if (declarations.length === 1) {
varDecl.parentPath.remove();
} else {
varDecl.remove();
}
varsHoistPoint.insertBefore(
variableDeclaration(kind, [varDecl.node]),
);
}
varsHoistPoint.insertBefore(
variableDeclaration(kind, [varDecl.node]),
);
}
}
}
},
});
},
};
}
/* eslint-enable */
1 change: 1 addition & 0 deletions packages/babel-preset-jest/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
src
tsconfig.json
tsconfig.tsbuildinfo
api-extractor.json
1 change: 1 addition & 0 deletions packages/diff-sequences/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
src
tsconfig.json
tsconfig.tsbuildinfo
api-extractor.json
6 changes: 3 additions & 3 deletions packages/diff-sequences/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,12 +779,12 @@ const validateCallback = (name: string, arg: unknown) => {
// Given lengths of sequences and input function to compare items at indexes,
// return by output function the number of adjacent items and starting indexes
// of each common subsequence.
export default (
export default function diffSequence(
aLength: number,
bLength: number,
isCommon: IsCommon,
foundSubsequence: FoundSubsequence,
): void => {
): void {
validateLength('aLength', aLength);
validateLength('bLength', bLength);
validateCallback('isCommon', isCommon);
Expand Down Expand Up @@ -869,4 +869,4 @@ export default (
foundSubsequence(nCommonR, aEnd, bEnd);
}
}
};
}
1 change: 1 addition & 0 deletions packages/expect/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ __typetests__
src
tsconfig.json
tsconfig.tsbuildinfo
api-extractor.json
1 change: 1 addition & 0 deletions packages/expect/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ interface AsymmetricMatchers {
any(sample: unknown): AsymmetricMatcher;
anything(): AsymmetricMatcher;
arrayContaining(sample: Array<unknown>): AsymmetricMatcher;
closeTo(sample: number, precision?: number): AsymmetricMatcher;
objectContaining(sample: Record<string, unknown>): AsymmetricMatcher;
stringContaining(sample: string): AsymmetricMatcher;
stringMatching(sample: string | RegExp): AsymmetricMatcher;
Expand Down
1 change: 1 addition & 0 deletions packages/jest-changed-files/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
src
tsconfig.json
tsconfig.tsbuildinfo
api-extractor.json
1 change: 1 addition & 0 deletions packages/jest-circus/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
src
tsconfig.json
tsconfig.tsbuildinfo
api-extractor.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import {

export type Expect = typeof expect;

export default (config: Pick<Config.GlobalConfig, 'expand'>): Expect => {
export default function jestExpect(
config: Pick<Config.GlobalConfig, 'expand'>,
): Expect {
expect.setState({expand: config.expand});
expect.extend({
toMatchInlineSnapshot,
Expand All @@ -29,4 +31,4 @@ export default (config: Pick<Config.GlobalConfig, 'expand'>): Expect => {
expect.addSnapshotSerializer = addSerializer;

return expect;
};
}
1 change: 1 addition & 0 deletions packages/jest-cli/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
src
tsconfig.json
tsconfig.tsbuildinfo
api-extractor.json
6 changes: 3 additions & 3 deletions packages/jest-cli/src/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ type PromptsResults = {

const getConfigFilename = (ext: string) => JEST_CONFIG_BASE_NAME + ext;

export default async (
export default async function init(
rootDir: string = tryRealpath(process.cwd()),
): Promise<void> => {
): Promise<void> {
// prerequisite checks
const projectPackageJsonPath: string = path.join(rootDir, PACKAGE_JSON);

Expand Down Expand Up @@ -152,4 +152,4 @@ export default async (
console.log(
`📝 Configuration file created at ${chalk.cyan(jestConfigPath)}`,
);
};
}
Loading

0 comments on commit ef89ae8

Please sign in to comment.