Skip to content

Commit

Permalink
tests: add test for missing packages (#2296)
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh3112 committed Jan 9, 2021
1 parent caa3348 commit ff2b2b3
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 3 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/nodejs.yml
Expand Up @@ -87,12 +87,16 @@ jobs:
- name: Install webpack ${{ matrix.webpack-version }}
run: yarn add -W webpack@${{ matrix.webpack-version }}

- name: Build
run: yarn build:ci
- name: Build and Bootstrap
run: |
yarn build:ci
yarn run lerna bootstrap
- name: Run Smoketests
run: yarn test:smoketests

- name: Test and Generate Coverage
run: |
yarn run lerna bootstrap
yarn prepsuite
yarn test:coverage
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -37,6 +37,7 @@
"prepsuite": "node scripts/prepareSuite.js",
"pretest": "yarn build && yarn lint && yarn prepsuite",
"test": "jest --reporters=default",
"test:smoketests": "nyc node smoketests",
"test:coverage": "nyc jest --forceExit",
"test:cli": "jest test --reporters=default --forceExit",
"test:packages": "jest packages/ --reporters=default --forceExit",
Expand Down
20 changes: 20 additions & 0 deletions smoketests/index.js
@@ -0,0 +1,20 @@
/* eslint-disable node/no-unpublished-require */
const tests = [require('./missing-packages/webpack-dev-server.test.js'), require('./missing-packages/webpack.test.js')];

(async () => {
let isAllPassed = true;
for await (const test of tests) {
console.log(`\nRUN ${test.name}`);
const isPass = await test.run();
if (!isPass) {
console.log(`FAIL ${test.name}`);
isAllPassed = false;
} else {
console.log(`PASS ${test.name}`);
}
}
if (!isAllPassed) {
process.exit(2);
}
process.exit(0);
})();
75 changes: 75 additions & 0 deletions smoketests/missing-packages/webpack-dev-server.test.js
@@ -0,0 +1,75 @@
const path = require('path');
const execa = require('execa');
const { renameSync } = require('fs');
const stripAnsi = require('strip-ansi');

const ROOT = process.env.GITHUB_WORKSPACE ? process.env.GITHUB_WORKSPACE : path.resolve(__dirname, '../../');
const CLI_ENTRY_PATH = path.resolve(ROOT, './packages/webpack-cli/bin/cli.js');

const getPkgPath = (pkg) => {
return path.resolve(ROOT, `./node_modules/${pkg}`);
};

const swapPkgName = (current, next) => {
console.log(` swapping ${current} with ${next}`);
renameSync(getPkgPath(current), getPkgPath(next));
};

const runTest = () => {
// Simulate package missing
swapPkgName('webpack-dev-server', '.webpack-dev-server');

const proc = execa(CLI_ENTRY_PATH, ['serve'], {
cwd: __dirname,
});

proc.stdin.setDefaultEncoding('utf-8');

proc.stdout.on('data', (chunk) => {
console.log(` stdout: ${chunk.toString()}`);
});

return new Promise((resolve) => {
setTimeout(() => {
console.log(' timeout: killing process');
proc.kill();
}, 5000);

const logMessage = "For using 'serve' command you need to install: 'webpack-dev-server' package";
const prompt = 'Would you like to install';
let hasLogMessage = false,
hasPrompt = false,
hasPassed = false;

proc.stderr.on('data', (chunk) => {
let data = stripAnsi(chunk.toString());
console.log(` stderr: ${data}`);

if (data.includes(logMessage)) {
hasLogMessage = true;
}

if (data.includes(prompt)) {
hasPrompt = true;
}

if (hasLogMessage && hasPrompt) {
hasPassed = true;
proc.kill();
}
});

proc.on('exit', () => {
swapPkgName('.webpack-dev-server', 'webpack-dev-server');
resolve(hasPassed);
});

proc.on('error', () => {
swapPkgName('.webpack-dev-server', 'webpack-dev-server');
resolve(false);
});
});
};

module.exports.run = runTest;
module.exports.name = 'Missing webpack-dev-server';
74 changes: 74 additions & 0 deletions smoketests/missing-packages/webpack.test.js
@@ -0,0 +1,74 @@
const path = require('path');
const execa = require('execa');
const { renameSync } = require('fs');
const stripAnsi = require('strip-ansi');

const ROOT = process.env.GITHUB_WORKSPACE ? process.env.GITHUB_WORKSPACE : path.resolve(__dirname, '../../');
const CLI_ENTRY_PATH = path.resolve(ROOT, './packages/webpack-cli/bin/cli.js');

const getPkgPath = (pkg) => {
return path.resolve(ROOT, `./node_modules/${pkg}`);
};

const swapPkgName = (current, next) => {
console.log(` swapping ${current} with ${next}`);
renameSync(getPkgPath(current), getPkgPath(next));
};

const runTest = () => {
// Simulate package missing
swapPkgName('webpack', '.webpack');

const proc = execa(CLI_ENTRY_PATH, [], {
cwd: __dirname,
});

proc.stdin.setDefaultEncoding('utf-8');

proc.stdout.on('data', (chunk) => {
console.log(` stdout: ${chunk.toString()}`);
});

return new Promise((resolve) => {
setTimeout(() => {
proc.kill();
}, 5000);

const logMessage = 'It looks like webpack is not installed.';
const prompt = 'Would you like to install';
let hasLogMessage = false,
hasPrompt = false,
hasPassed = false;

proc.stderr.on('data', (chunk) => {
let data = stripAnsi(chunk.toString());
console.log(` stderr: ${data}`);

if (data.includes(logMessage)) {
hasLogMessage = true;
}

if (data.includes(prompt)) {
hasPrompt = true;
}

if (hasLogMessage && hasPrompt) {
hasPassed = true;
proc.kill();
}
});

proc.on('exit', () => {
swapPkgName('.webpack', 'webpack');
resolve(hasPassed);
});

proc.on('error', () => {
swapPkgName('.webpack', 'webpack');
resolve(false);
});
});
};

module.exports.run = runTest;
module.exports.name = 'Missing webpack';

0 comments on commit ff2b2b3

Please sign in to comment.