Skip to content

Commit

Permalink
fix(tests): detect npm version to use or not ci
Browse files Browse the repository at this point in the history
  • Loading branch information
huafu committed Aug 8, 2018
1 parent 3e4de3e commit 683a1e5
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 51 deletions.
5 changes: 0 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,5 @@ node_js:
- "6"
before_install:
- sudo sysctl fs.inotify.max_user_watches=524288
- npm i -g greenkeeper-lockfile@1 rimraf
before_script:
# - greenkeeper-lockfile-update
script:
- npm test
after_script:
# - greenkeeper-lockfile-upload
1 change: 0 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ install:
- ps: Install-Product node $env:nodejs_version
- set CI=true
- set AppVeyor=true
- npm i -g rimraf
- npm ci || npm install

build: off
Expand Down
37 changes: 19 additions & 18 deletions e2e/__helpers__/test-case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,24 +130,25 @@ export default function configureTestCase(
}

export function sanitizeOutput(output: string): string {
return (
output
.trim()
// removes total and estimated times
.replace(
/^(\s*Time\s*:\s*)[\d.]+m?s(?:(,\s*estimated\s+)[\d.]+m?s)?(\s*)$/gm,
(_, start, estimatedPrefix, end) => {
return `${start}XXs${
estimatedPrefix ? `${estimatedPrefix}YYs` : ''
}${end}`;
},
)
// removes each test time values
.replace(
/^(\s*(?:✕|✓)\s+.+\s+\()[\d.]+m?s(\)\s*)$/gm,
(_, start, end) => `${start}XXms${end}`,
)
);
let out: string = output
.trim()
// removes total and estimated times
.replace(
/^(\s*Time\s*:\s*)[\d.]+m?s(?:(,\s*estimated\s+)[\d.]+m?s)?(\s*)$/gm,
(_, start, estimatedPrefix, end) => {
return `${start}XXs${end}`;
},
)
// removes each test time values
.replace(
/^(\s*(?:✕|×|✓|√)\s+.+\s+\()[\d.]+m?s(\)\s*)$/gm,
(_, start, end) => `${start}XXms${end}`,
);
// TODO: improves this...
if (process.platform === 'win32') {
out = out.replace(/\\/g, '/');
}
return out;
}

export function run(
Expand Down
6 changes: 3 additions & 3 deletions e2e/__tests__/__snapshots__/source-map.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Object {
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: XXs, estimated YYs
Time: XXs
Ran all test suites.
================================================================================,
"with-babel-7": jest exit code: 1
Expand Down Expand Up @@ -65,7 +65,7 @@ Object {
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: XXs, estimated YYs
Time: XXs
Ran all test suites.
================================================================================,
"with-jest-22": jest exit code: 1
Expand Down Expand Up @@ -97,7 +97,7 @@ Object {
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: XXs, estimated YYs
Time: XXs
Ran all test suites.
================================================================================,
}
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
"types": "./dist/index.d.ts",
"description": "A preprocessor with sourcemap support to help use Typescript with Jest",
"scripts": {
"prebuild": "node scripts/clean-dist.js",
"build": "tsc -p tsconfig.build.json",
"build:watch": "tsc -p tsconfig.build.json -w",
"clean": "node scripts/clean.js",
"pretest": "npm run tslint",
"pretest": "npm run lint",
"test": "node scripts/e2e.js",
"tslint": "tslint 'src/**/*.ts'",
"lint": "tslint 'src/**/*.ts'",
"doc": "doctoc .",
"prepare": "rimraf dist && npm run build",
"prepare": "npm run build",
"prepublishOnly": "npm run test",
"precommit": "lint-staged",
"postcommit": "git reset",
Expand Down Expand Up @@ -54,7 +55,7 @@
"lint-staged": "^7.2.0",
"prettier": "^1.14.1",
"reflect-metadata": "^0.1.12",
"rimraf": "^2.6.2",
"semver": "^5.5.0",
"tslint": "^5.11.0",
"typescript": "^2.9.2"
},
Expand Down
6 changes: 6 additions & 0 deletions scripts/clean-dist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env node

const { removeSync } = require('fs-extra');
const Paths = require('./paths');

removeSync(Paths.distDir);
18 changes: 9 additions & 9 deletions scripts/clean.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/usr/bin/env node

const rimraf = require('rimraf');
const { removeSync } = require('fs-extra');
const Paths = require('./paths');
const { join } = require('path');

rimraf.sync(Paths.distDir);
rimraf.sync(join(Paths.testsRootDir, '*', 'coverage'));
rimraf.sync(join(Paths.testsRootDir, '*', 'debug.txt'));
rimraf.sync(join(Paths.testsRootDir, '*', 'node_modules'));
rimraf.sync(join(Paths.e2eSourceDir, '*', 'node_modules'));
rimraf.sync(join(Paths.e2eTemplatesDir, '*', 'node_modules'));
rimraf.sync(Paths.e2eWorkDir);
rimraf.sync(Paths.e2eWotkDirLink);
removeSync(Paths.distDir);
removeSync(join(Paths.testsRootDir, '*', 'coverage'));
removeSync(join(Paths.testsRootDir, '*', 'debug.txt'));
removeSync(join(Paths.testsRootDir, '*', 'node_modules'));
removeSync(join(Paths.e2eSourceDir, '*', 'node_modules'));
removeSync(join(Paths.e2eTemplatesDir, '*', 'node_modules'));
removeSync(Paths.e2eWorkDir);
removeSync(Paths.e2eWotkDirLink);
25 changes: 14 additions & 11 deletions scripts/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ const { sync: spawnSync } = require('cross-spawn');
const fs = require('fs-extra');
const path = require('path');
const Paths = require('./paths');
const { satisfies } = require('semver');

const NodeVersion = (versions => {
return { major: versions[0], minor: versions[1], patch: versions[2] };
})(
process.versions.node
.split('-')
.shift()
.split('.')
.map(s => parseInt(s, 10))
);
const npmVersion = spawnSync('npm', ['-s', '--version'])
.stdout.toString()
.trim();
const npmHasCiCommand = satisfies(npmVersion, '>=5.7.0');
const npmHasPrepare = satisfies(npmVersion, '>=4.0.0');

function getDirectories(rootDir) {
return fs.readdirSync(rootDir).filter(function(file) {
Expand All @@ -28,6 +25,13 @@ function getDirectories(rootDir) {
function setupE2e() {
// this will trigger the build as well (not using yarn since yarn pack is bugy)
// keep on to so that the build is triggered beforehand (pack => prepublish => clean-build => build)
// Except that on npm < 4.0.0 the prepare doesn't exists
if (!npmHasPrepare) {
spawnSync('npm', ['-s', 'run', 'build'], {
cwd: Paths.rootDir,
stdio: 'inherit',
});
}
const res = spawnSync('npm', ['-s', 'pack'], { cwd: Paths.rootDir });
const bundle = path.join(Paths.rootDir, res.stdout.toString().trim());

Expand All @@ -53,10 +57,9 @@ function setupE2e() {
// template dir, to know if we should re-install or not
if (fs.existsSync(path.join(dir, 'node_modules'))) return;

if (NodeVersion.major >= 8) {
if (npmHasCiCommand) {
spawnSync('npm', ['ci'], { cwd: dir, stdio: 'inherit' });
} else {
// npm coming with node < 8 does not have the `ci` command
spawnSync('npm', ['i'], { cwd: dir, stdio: 'inherit' });
}
spawnSync('npm', ['i', '-D', bundle], { cwd: dir, stdio: 'inherit' });
Expand Down

0 comments on commit 683a1e5

Please sign in to comment.