[now-cli] Install dependencies before running now dev in tests (#2948)

* [now-cli] Install dependencies before running `now dev` in tests

* Check install exit code

* Add more logging

* Add more logging

* More logging

* Include yarn.lock file

* Add lock files to dev fixtures

* Ignore test

* Ignore another test

* Ignore another test

* Whitespace

* Install deps for unit tests

* Whitespace
This commit is contained in:
Andy
2019-09-03 20:05:21 +02:00
committed by Andy Bitz
parent 3ef27ae45c
commit 658b9e9007
26 changed files with 112601 additions and 73 deletions

1
.gitignore vendored
View File

@@ -12,6 +12,7 @@ packages/now-cli/.builders
packages/now-cli/assets
packages/now-cli/src/util/dev/templates/*.ts
packages/now-cli/test/**/yarn.lock
!packages/now-cli/test/dev/**/yarn.lock
packages/now-cli/test/**/node_modules
packages/now-cli/test/dev/fixtures/08-hugo/hugo
packages/now-cli/test/dev/fixtures/**/dist

View File

@@ -1,5 +1,4 @@
import test from 'ava';
import { filterPackage } from '../src/util/dev/builder-cache';
test('[dev-builder] filter install "latest", cached canary', async t => {

View File

@@ -1,5 +1,4 @@
import test from 'ava';
import devRouter from '../src/util/dev/router';
test('[dev-router] 301 redirection', async t => {

View File

@@ -1,6 +1,8 @@
import url from 'url';
import test from 'ava';
import path from 'path';
import execa from 'execa';
import fs from 'fs-extra';
import fetch from 'node-fetch';
import listen from 'async-listen';
import { request, createServer } from 'http';
@@ -9,9 +11,20 @@ import DevServer from '../src/util/dev/server';
import { installBuilders, getBuildUtils } from '../src/util/dev/builder-cache';
import parseListen from '../src/util/dev/parse-listen';
async function runNpmInstall(fixturePath) {
if (await fs.exists(path.join(fixturePath, 'package.json'))) {
return execa('yarn', ['install'], { cwd: fixturePath });
}
}
function testFixture(name, fn) {
return async t => {
let server;
const fixturePath = path.join(__dirname, 'fixtures', 'unit', name);
await runNpmInstall(fixturePath);
try {
let readyResolve;
let readyPromise = new Promise(resolve => {
@@ -29,7 +42,6 @@ function testFixture(name, fn) {
origReady(msg);
};
const fixturePath = path.join(__dirname, `fixtures/unit/${name}`);
server = new DevServer(fixturePath, { output, debug });
await server.start(0);

View File

@@ -0,0 +1,8 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
moment@^2.24.0:
version "2.24.0"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.24.0.tgz#0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b"
integrity sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg==

File diff suppressed because it is too large Load Diff

View File

@@ -34,3 +34,5 @@ node_modules
/public
/test/coverage-jest
/test/coverage-karma
!yarn.lock

View File

@@ -1,2 +1 @@
README.md
yarn.lock

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -53,11 +53,19 @@ async function exec(directory, args = []) {
});
}
async function runNpmInstall(fixturePath) {
if (await fs.exists(path.join(fixturePath, 'package.json'))) {
return execa('yarn', ['install'], { cwd: fixturePath });
}
}
function formatOutput({ stderr, stdout }) {
return `Received:\n"${stderr}"\n"${stdout}"`;
}
function testFixture(directory, opts = {}, args = []) {
async function testFixture(directory, opts = {}, args = []) {
await runNpmInstall(directory);
port = ++port;
return {
dev: execa(binaryPath, ['dev', directory, '-l', String(port), ...args], {
@@ -74,6 +82,9 @@ function testFixtureStdio(directory, fn) {
return async t => {
let dev;
const dir = fixture(directory);
await runNpmInstall(dir);
try {
port = ++port;
let output = '';
@@ -112,7 +123,10 @@ test('[now dev] validate builds', async t => {
const output = await exec(directory);
t.is(output.code, 1, formatOutput(output));
t.regex(output.stderr, /Invalid `builds` property: \[0\]\.src should be string/gm);
t.regex(
output.stderr,
/Invalid `builds` property: \[0\]\.src should be string/gm
);
});
test('[now dev] validate routes', async t => {
@@ -120,12 +134,15 @@ test('[now dev] validate routes', async t => {
const output = await exec(directory);
t.is(output.code, 1, formatOutput(output));
t.regex(output.stderr, /Invalid `routes` property: \[0\]\.src should be string/gm);
t.regex(
output.stderr,
/Invalid `routes` property: \[0\]\.src should be string/gm
);
});
test('[now dev] 00-list-directory', async t => {
const directory = fixture('00-list-directory');
const { dev, port } = testFixture(directory);
const { dev, port } = await testFixture(directory);
try {
// start `now dev` detached in child_process
@@ -147,7 +164,7 @@ test('[now dev] 00-list-directory', async t => {
test('[now dev] 01-node', async t => {
const directory = fixture('01-node');
const { dev, port } = testFixture(directory);
const { dev, port } = await testFixture(directory);
try {
// start `now dev` detached in child_process
@@ -169,7 +186,9 @@ test('[now dev] 01-node', async t => {
if (satisfies(process.version, '10.x')) {
test('[now dev] 02-angular-node', async t => {
const directory = fixture('02-angular-node');
const { dev, port } = testFixture(directory, { stdio: 'pipe' }, ['--debug']);
const { dev, port } = await testFixture(directory, { stdio: 'pipe' }, [
'--debug'
]);
let stderr = '';
@@ -256,7 +275,7 @@ test(
test('[now dev] 07-hexo-node', async t => {
const directory = fixture('07-hexo-node');
const { dev, port } = testFixture(directory);
const { dev, port } = await testFixture(directory);
try {
// start `now dev` detached in child_process
@@ -289,7 +308,7 @@ test(
test('[now dev] 10-nextjs-node', async t => {
const directory = fixture('10-nextjs-node');
const { dev, port } = testFixture(directory);
const { dev, port } = await testFixture(directory);
try {
// start `now dev` detached in child_process
@@ -309,7 +328,7 @@ test('[now dev] 10-nextjs-node', async t => {
// test('[now dev] 11-nuxtjs-node', async t => {
// const directory = fixture('11-nuxtjs-node');
// const { dev, port } = testFixture(directory);
// const { dev, port } = await testFixture(directory);
// try {
// // start `now dev` detached in child_process
@@ -330,7 +349,7 @@ test('[now dev] 10-nextjs-node', async t => {
test('[now dev] 12-polymer-node', async t => {
const directory = fixture('12-polymer-node');
const { dev, port } = testFixture(directory);
const { dev, port } = await testFixture(directory);
try {
// start `now dev` detached in child_process
@@ -350,7 +369,7 @@ test('[now dev] 12-polymer-node', async t => {
test('[now dev] 13-preact-node', async t => {
const directory = fixture('13-preact-node');
const { dev, port } = testFixture(directory);
const { dev, port } = await testFixture(directory);
try {
// start `now dev` detached in child_process
@@ -370,7 +389,7 @@ test('[now dev] 13-preact-node', async t => {
test('[now dev] 14-svelte-node', async t => {
const directory = fixture('14-svelte-node');
const { dev, port } = testFixture(directory);
const { dev, port } = await testFixture(directory);
try {
// start `now dev` detached in child_process
@@ -390,7 +409,7 @@ test('[now dev] 14-svelte-node', async t => {
// test('[now dev] 15-umijs-node', async t => {
// const directory = fixture('15-umijs-node');
// const { dev, port } = testFixture(directory);
// const { dev, port } = await testFixture(directory);
// try {
// // start `now dev` detached in child_process
@@ -409,49 +428,54 @@ test('[now dev] 14-svelte-node', async t => {
// }
// });
test('[now dev] 16-vue-node', async t => {
const directory = fixture('16-vue-node');
const { dev, port } = testFixture(directory);
if (satisfies(process.version, '^8.12.0 || >=9.7.0')) {
test('[now dev] 16-vue-node', async t => {
const directory = fixture('16-vue-node');
const { dev, port } = await testFixture(directory);
try {
// start `now dev` detached in child_process
dev.unref();
try {
// start `now dev` detached in child_process
dev.unref();
const result = await fetchWithRetry(`http://localhost:${port}`, 180);
const response = await result;
const result = await fetchWithRetry(`http://localhost:${port}`, 180);
const response = await result;
validateResponseHeaders(t, response);
validateResponseHeaders(t, response);
const body = await response.text();
t.regex(body, /Vue.js \+ Node.js API/gm);
} finally {
dev.kill('SIGTERM');
}
});
const body = await response.text();
t.regex(body, /Vue.js \+ Node.js API/gm);
} finally {
dev.kill('SIGTERM');
}
});
test('[now dev] 17-vuepress-node', async t => {
const directory = fixture('17-vuepress-node');
const { dev, port } = await testFixture(directory);
test('[now dev] 17-vuepress-node', async t => {
const directory = fixture('17-vuepress-node');
const { dev, port } = testFixture(directory);
try {
// start `now dev` detached in child_process
dev.unref();
try {
// start `now dev` detached in child_process
dev.unref();
const result = await fetchWithRetry(`http://localhost:${port}`, 180);
const response = await result;
const result = await fetchWithRetry(`http://localhost:${port}`, 180);
const response = await result;
validateResponseHeaders(t, response);
validateResponseHeaders(t, response);
const body = await response.text();
t.regex(body, /VuePress \+ Node.js API/gm);
} finally {
dev.kill('SIGTERM');
}
});
const body = await response.text();
t.regex(body, /VuePress \+ Node.js API/gm);
} finally {
dev.kill('SIGTERM');
}
});
} else {
console.log(
'Skipping `10-vue-node` and `17-vuepress-node` test since it requires Node ^8.12.0 || >=9.7.0'
);
}
test('[now dev] double slashes redirect', async t => {
const directory = fixture('01-node');
const { dev, port } = testFixture(directory);
const { dev, port } = await testFixture(directory);
try {
// start `now dev` detached in child_process
@@ -596,7 +620,7 @@ test(
test('[now dev] temporary directory listing', async t => {
const directory = fixture('temporary-directory-listing');
const { dev, port } = testFixture(directory);
const { dev, port } = await testFixture(directory);
try {
// start `now dev` detached in child_process
@@ -629,7 +653,7 @@ test('[now dev] temporary directory listing', async t => {
test('[now dev] add a `package.json` to trigger `@now/static-build`', async t => {
const directory = fixture('trigger-static-build');
const { dev, port } = testFixture(directory);
const { dev, port } = await testFixture(directory);
try {
dev.unref();
@@ -666,7 +690,7 @@ test('[now dev] add a `package.json` to trigger `@now/static-build`', async t =>
test('[now dev] no build matches warning', async t => {
const directory = fixture('no-build-matches');
const { dev } = testFixture(directory, {
const { dev } = await testFixture(directory, {
stdio: ['ignore', 'pipe', 'pipe']
});
@@ -689,33 +713,39 @@ test('[now dev] no build matches warning', async t => {
}
});
test('[now dev] do not recursivly check the path', async t => {
const directory = fixture('handle-filesystem-missing');
const { dev, port } = testFixture(directory);
if (satisfies(process.version, '^8.10.0 || ^10.13.0 || >=11.10.1')) {
test('[now dev] do not recursivly check the path', async t => {
const directory = fixture('handle-filesystem-missing');
const { dev, port } = await testFixture(directory);
try {
dev.unref();
try {
dev.unref();
{
const response = await fetchWithRetry(`http://localhost:${port}`, 180);
validateResponseHeaders(t, response);
const body = await response.text();
t.is(body.trim(), 'hello');
{
const response = await fetchWithRetry(`http://localhost:${port}`, 180);
validateResponseHeaders(t, response);
const body = await response.text();
t.is(body.trim(), 'hello');
}
{
const response = await fetch(`http://localhost:${port}/favicon.txt`);
validateResponseHeaders(t, response);
t.is(response.status, 404);
}
} finally {
dev.kill('SIGTERM');
}
{
const response = await fetch(`http://localhost:${port}/favicon.txt`);
validateResponseHeaders(t, response);
t.is(response.status, 404);
}
} finally {
dev.kill('SIGTERM');
}
});
});
} else {
console.log(
'Skipping `do not recursivly check the path` test since it requires Node ^8.10.0 || ^10.13.0 || >=11.10.1'
);
}
test('[now dev] render warning for empty cwd dir', async t => {
const directory = fixture('empty');
const { dev, port } = testFixture(directory, {
const { dev, port } = await testFixture(directory, {
stdio: ['ignore', 'pipe', 'pipe']
});