From 71fbd6149a00c304202c2f29937fffceaf06eb56 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sat, 18 Apr 2020 01:24:22 +0800 Subject: [PATCH] Refactor production tests (#8073) # Conflicts: # scripts/test-dist.js --- .github/workflows/prod-test.yml | 20 ++++++++++++----- jest.config.js | 6 +++++ package.json | 3 ++- scripts/install-prettier.js | 23 +++++++++++++++++++ scripts/test-dist.js | 39 --------------------------------- 5 files changed, 45 insertions(+), 46 deletions(-) create mode 100644 scripts/install-prettier.js delete mode 100644 scripts/test-dist.js diff --git a/.github/workflows/prod-test.yml b/.github/workflows/prod-test.yml index e49fe70d0..a485f7ac7 100644 --- a/.github/workflows/prod-test.yml +++ b/.github/workflows/prod-test.yml @@ -99,8 +99,13 @@ jobs: name: dist path: dist - - name: Run Tests - run: yarn test:dist + - name: Run Tests (macOS) + if: matrix.os == 'macos-latest' + run: yarn test:dist --maxWorkers=4 + + - name: Run Tests (Linux and Windows) + if: matrix.os != 'macos-latest' + run: yarn test:dist --maxWorkers=2 standalone: strategy: @@ -117,8 +122,6 @@ jobs: exclude: - os: "macos-latest" node: "13" - env: - STANDALONE: true name: Node.js ${{ matrix.node }} on ${{ matrix.os }} (standalone) runs-on: ${{ matrix.os }} needs: [build] @@ -142,5 +145,10 @@ jobs: name: dist path: dist - - name: Run Tests - run: yarn test:dist + - name: Run Tests (macOS) + if: matrix.os == 'macos-latest' + run: yarn test:dist-standalone --maxWorkers=4 + + - name: Run Tests (Linux and Windows) + if: matrix.os != 'macos-latest' + run: yarn test:dist-standalone --maxWorkers=2 diff --git a/jest.config.js b/jest.config.js index bfa41d6af..91ae0e3ee 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,5 +1,11 @@ "use strict"; + +const installPrettier = require("./scripts/install-prettier"); + const ENABLE_CODE_COVERAGE = !!process.env.ENABLE_CODE_COVERAGE; +if (process.env.NODE_ENV === "production") { + process.env.PRETTIER_DIR = installPrettier(); +} module.exports = { setupFiles: ["/tests_config/run_spec.js"], diff --git a/package.json b/package.json index 2e27f680e..bdf1f7425 100644 --- a/package.json +++ b/package.json @@ -115,7 +115,8 @@ "prepublishOnly": "echo \"Error: must publish from dist/\" && exit 1", "prepare-release": "yarn && yarn build && yarn test:dist", "test": "jest", - "test:dist": "node ./scripts/test-dist.js", + "test:dist": "cross-env NODE_ENV=production jest", + "test:dist-standalone": "cross-env NODE_ENV=production STANDALONE=1 jest tests/", "test:integration": "jest tests_integration", "perf:repeat": "yarn && yarn build && cross-env NODE_ENV=production node ./dist/bin-prettier.js --debug-repeat ${PERF_REPEAT:-1000} --loglevel debug ${PERF_FILE:-./index.js} > /dev/null", "perf:repeat-inspect": "yarn && yarn build && cross-env NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --debug-repeat ${PERF_REPEAT:-1000} --loglevel debug ${PERF_FILE:-./index.js} > /dev/null", diff --git a/scripts/install-prettier.js b/scripts/install-prettier.js new file mode 100644 index 000000000..449c5f451 --- /dev/null +++ b/scripts/install-prettier.js @@ -0,0 +1,23 @@ +"use strict"; + +const path = require("path"); +const shell = require("shelljs"); +const tempy = require("tempy"); + +shell.config.fatal = true; + +const rootDir = path.join(__dirname, ".."); +const distDir = path.join(rootDir, "dist"); + +module.exports = () => { + const file = shell.exec("npm pack", { cwd: distDir }).stdout.trim(); + const tarPath = path.join(distDir, file); + const tmpDir = tempy.directory(); + + shell.config.silent = true; + shell.exec("npm init -y", { cwd: tmpDir }); + shell.exec(`npm install "${tarPath}"`, { cwd: tmpDir }); + shell.config.silent = false; + + return path.join(tmpDir, "node_modules/prettier"); +}; diff --git a/scripts/test-dist.js b/scripts/test-dist.js deleted file mode 100644 index a915e9afe..000000000 --- a/scripts/test-dist.js +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env node - -"use strict"; - -const path = require("path"); -const shell = require("shelljs"); -const tempy = require("tempy"); -const { isCI } = require("ci-info"); - -shell.config.fatal = true; - -const rootDir = path.join(__dirname, ".."); -const distDir = path.join(rootDir, "dist"); - -const file = shell.exec("npm pack", { cwd: distDir }).stdout.trim(); -const tarPath = path.join(distDir, file); -const tmpDir = tempy.directory(); - -shell.config.silent = true; -shell.exec("npm init -y", { cwd: tmpDir }); -shell.exec(`npm install "${tarPath}"`, { cwd: tmpDir }); -shell.config.silent = false; - -const runInBand = isCI ? "--runInBand" : ""; -const testPath = process.env.TEST_STANDALONE ? "tests/" : ""; -const cmd = `yarn test --color ${runInBand} ${testPath}`; - -const { code } = shell.exec(cmd, { - cwd: rootDir, - env: { - ...process.env, - NODE_ENV: "production", - AST_COMPARE: "1", - PRETTIER_DIR: path.join(tmpDir, "node_modules/prettier"), - }, - shell: true, -}); - -process.exit(code);