Skip to content

Commit

Permalink
Upgrade to latest Jest and faster tests (#6053)
Browse files Browse the repository at this point in the history
* Faster Unit tests

* Fix fake timers

* Remove logHeapUsage
  • Loading branch information
ardatan authored May 26, 2021
1 parent f979313 commit 71256e7
Show file tree
Hide file tree
Showing 13 changed files with 1,153 additions and 847 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/canary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ jobs:
uses: actions/cache@v2
with:
path: '**/node_modules'
key: ${{ runner.os }}-14-yarn-${{ hashFiles('yarn.lock') }}
key: ${{ runner.os }}-16-yarn-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-14-yarn-
${{ runner.os }}-16-yarn-
- name: Install Dependencies using Yarn
run: yarn install --ignore-engines && git checkout yarn.lock
- name: Release Canary
Expand Down
9 changes: 4 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ on:

jobs:
test:
name: Testing on ${{matrix.os}} and Node ${{matrix.node_version}}
runs-on: ${{matrix.os}}
name: Testing on Node ${{matrix.node_version}}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
node_version: [10, 16]
node_version: [12, 16]
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down Expand Up @@ -45,7 +44,7 @@ jobs:
- name: Build
run: yarn build
- name: Test
run: yarn test --logHeapUsage
run: yarn test
env:
CI: true
- name: Test dev-tests
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ jobs:
uses: actions/cache@v2
with:
path: '**/node_modules'
key: ${{ runner.os }}-14-15-yarn-${{ hashFiles('yarn.lock') }}
key: ${{ runner.os }}-16-yarn-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-14-15-yarn-
${{ runner.os }}-16-yarn-
- name: Install Dependencies using Yarn
run: yarn install && git checkout yarn.lock
- name: Create Release Pull Request or Publish to npm
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ jobs:
uses: actions/setup-node@master
with:
node-version: 16
- name: Cache Yarn
uses: actions/cache@v2
with:
path: '**/node_modules'
key: ${{ runner.os }}-${{matrix.node_version}}-yarn-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-${{matrix.node_version}}-yarn-
- name: Install Dependencies
run: yarn install
- name: Build Plugins
Expand Down
3 changes: 3 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: [['@babel/preset-env', { targets: { node: 'current' } }], '@babel/preset-typescript'],
};
9 changes: 2 additions & 7 deletions jest.project.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,16 @@ module.exports = ({ dirname, projectMode = true }) => {

return {
...(CI || !projectMode ? {} : { displayName: pkg.name.replace('@graphql-codegen/', '') }),
transform: { '^.+\\.tsx?$': 'ts-jest' },
transform: { '^.+\\.tsx?$': 'babel-jest' },
testEnvironment: 'node',
rootDir: dirname,
globals: {
'ts-jest': {
diagnostics: false,
tsconfig: 'tsconfig.json',
},
},
restoreMocks: true,
reporters: ['default'],
modulePathIgnorePatterns: ['dist'],
moduleNameMapper: pathsToModuleNameMapper(tsconfig.compilerOptions.paths, { prefix: `${ROOT_DIR}/` }),
cacheDirectory: resolve(ROOT_DIR, `${CI ? '' : 'node_modules/'}.cache/jest`),
setupFiles: [`${ROOT_DIR}/dev-test/setup.js`],
collectCoverage: false,
testTimeout: 20000,
};
};
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"prebuild": "rimraf packages/{*,plugins/*/*,presets/*,utils/*}/dist",
"build": "tsc --project tsconfig.json && bob build",
"watch-build": "npx tsc-watch --project tsconfig.json --onSuccess \"bob build\"",
"test": "jest --no-watchman",
"test": "jest --forceExit --no-watchman",
"lint": "eslint --ext .ts .",
"prettier": "prettier --ignore-path .gitignore --write --list-different \"**/*.{ts,tsx,graphql,yml}\"",
"prettier:check": "prettier --ignore-path .gitignore --check \"**/*.{ts,tsx,graphql,yml}\"",
Expand Down Expand Up @@ -39,6 +39,8 @@
},
"devDependencies": {
"@apollo/client": "3.3.19",
"@babel/core": "7.14.3",
"@babel/preset-env": "7.14.2",
"@babel/preset-typescript": "7.13.0",
"@changesets/cli": "2.16.0",
"@graphql-typed-document-node/core": "3.1.0",
Expand All @@ -55,6 +57,7 @@
"apollo-client": "2.6.10",
"apollo-server": "2.24.1",
"auto-bind": "4.0.0",
"babel-jest": "27.0.1",
"bob-the-bundler": "1.2.0",
"eslint": "7.27.0",
"eslint-config-prettier": "8.3.0",
Expand All @@ -71,8 +74,8 @@
"graphql-tag": "2.12.4",
"husky": "6.0.0",
"java-ast": "0.3.0",
"jest": "26.6.3",
"jest-docblock": "26.0.0",
"jest": "27.0.1",
"jest-docblock": "27.0.1",
"jest-junit": "12.1.0",
"lint-staged": "11.0.0",
"lodash": "4.17.21",
Expand All @@ -81,7 +84,7 @@
"react-query": "3.16.0",
"rimraf": "3.0.2",
"stencil-apollo": "0.1.6",
"ts-jest": "26.5.6",
"ts-jest": "27.0.0",
"tslib": "2.2.0",
"typescript": "4.2.4",
"urql": "2.0.3",
Expand All @@ -99,6 +102,8 @@
]
},
"resolutions": {
"babel-jest": "27.0.1",
"jest-runner": "27.0.1",
"graphql": "15.5.0",
"graphql-language-service-interface": "2.8.3",
"**/apollo-language-server/graphql": "^15.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-codegen-cli/tests/codegen.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Codegen Executor', () => {
monorepo.correctCWD();

beforeEach(() => {
jest.useFakeTimers();
jest.useFakeTimers('legacy');
});

describe('Generator General Options', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ describe('Apollo Angular', () => {
});

it('should output warning if documentMode = external and importDocumentNodeExternallyFrom is not set', async () => {
spyOn(console, 'warn');
jest.spyOn(console, 'warn');
const docs = [{ location: '', document: basicDoc }];
await plugin(
schema,
Expand All @@ -235,7 +235,7 @@ describe('Apollo Angular', () => {
});

it('output warning if importOperationTypesFrom is set to something other than "Operations"', async () => {
spyOn(console, 'warn');
jest.spyOn(console, 'warn');
const docs = [{ location: '', document: basicDoc }];
await plugin(
schema,
Expand All @@ -256,7 +256,7 @@ describe('Apollo Angular', () => {
});

it('output warning if importOperationTypesFrom is set and documentMode is not "external"', async () => {
spyOn(console, 'warn');
jest.spyOn(console, 'warn');
const docs = [{ location: '', document: basicDoc }];
await plugin(
schema,
Expand All @@ -276,7 +276,7 @@ describe('Apollo Angular', () => {
});

it('output warning if importOperationTypesFrom is set and importDocumentNodeExternallyFrom is not', async () => {
spyOn(console, 'warn');
jest.spyOn(console, 'warn');
const docs = [{ location: '', document: basicDoc }];
await plugin(
schema,
Expand Down
2 changes: 1 addition & 1 deletion packages/presets/graphql-modules/tests/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Integration', () => {
monorepo.correctCWD();

beforeEach(() => {
jest.useFakeTimers();
jest.useFakeTimers('legacy');
});

// In this test, we make sure executeCodegen passes on a list of Sources as an extension
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/graphql-codegen-testing/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { oneLine, stripIndent } from 'common-tags';
import { resolve } from 'path';
import { existsSync } from 'fs';
import diff from 'jest-diff';
import { diff } from 'jest-diff';

declare global {
// eslint-disable-next-line no-redeclare
Expand Down
46 changes: 46 additions & 0 deletions patches/babel-jest+27.0.1.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
diff --git a/node_modules/babel-jest/build/index.js b/node_modules/babel-jest/build/index.js
index f74b673..283655b 100644
--- a/node_modules/babel-jest/build/index.js
+++ b/node_modules/babel-jest/build/index.js
@@ -208,7 +208,7 @@ function loadBabelOptions(
jestTransformOptions
) {
const {options} = loadBabelConfig(cwd, filename, transformOptions);
- return addIstanbulInstrumentation(options, jestTransformOptions);
+ return addIstanbulInstrumentation(options, transformOptions);
}

async function loadBabelOptionsAsync(
@@ -218,7 +218,7 @@ async function loadBabelOptionsAsync(
jestTransformOptions
) {
const {options} = await loadBabelConfigAsync(cwd, filename, transformOptions);
- return addIstanbulInstrumentation(options, jestTransformOptions);
+ return addIstanbulInstrumentation(options, transformOptions);
}

const createTransformer = userOptions => {
@@ -320,11 +320,14 @@ const createTransformer = userOptions => {
);
},

- process(sourceText, sourcePath, transformOptions) {
+ process(sourceText, sourcePath, config, transformOptions) {
const babelOptions = loadBabelOptions(
- transformOptions.config.cwd,
+ config.cwd,
sourcePath,
- mergeBabelTransformOptions(sourcePath, transformOptions),
+ mergeBabelTransformOptions(sourcePath, {
+ ...transformOptions,
+ config
+ }),
transformOptions
);
const transformResult = (0, _core().transformSync)(
@@ -381,4 +384,4 @@ const transformer = {
createTransformer
};
var _default = transformer;
-exports.default = _default;
+module.exports = _default;
Loading

0 comments on commit 71256e7

Please sign in to comment.