Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
Fixed some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Giancarlo Anemone committed Jan 5, 2018
1 parent a0979f5 commit 9580cc0
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 123 deletions.
10 changes: 7 additions & 3 deletions commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ exports.builder = {
// default: false,
// describe: 'Debug application',
// },
port: {
type: 'number',
describe:
'Port to start the server on. Defaults to process.env.PORT_HTTP || 3000',
},
dir: {
type: 'string',
default: '.',
Expand All @@ -23,7 +28,7 @@ exports.builder = {
},
};

exports.run = async function({dir = '.', environment}) {
exports.run = async function({dir = '.', environment, port}) {
const getEntry = env => {
const entryPath = `.fusion/dist/${env}/server/server-main.js`;
return path.resolve(dir, entryPath);
Expand All @@ -36,8 +41,7 @@ exports.run = async function({dir = '.', environment}) {
if (env) {
const entry = getEntry(env);
const {start} = require(entry);

return start({port: process.env.PORT_HTTP || 3000}); // handle server bootstrap errors (e.g. port already in use)
return start({port: port || process.env.PORT_HTTP || 3000}); // handle server bootstrap errors (e.g. port already in use)
} else {
throw new Error(`App can't start. JS isn't compiled`); // handle compilation errors
}
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
},
"scripts": {
"lint": "eslint .",
"test": "node test/index.js && node test/cli/test.js",
"TODO(#102)": "Something weird is going on with nyc and unitest. This is a hack to fix it",
"cover": "node test/cli/test.js && nyc node test/index.js"
"test": "node test/index.js",
"cover": "nyc node test/index.js"
},
"dependencies": {
"@nadiia/file-loader": "^1.0.0-beta.5",
Expand Down
109 changes: 70 additions & 39 deletions test/cli/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
const fs = require('fs');
const path = require('path');
const test = require('tape');

const {run} = require('../../bin/cli-runner');
const {cmd, start} = require('../run-command');

test('`fusion build` works', async t => {
const dir = path.resolve(__dirname, '../fixtures/noop');
Expand All @@ -27,12 +26,11 @@ test('`fusion build` works', async t => {
dir,
`.fusion/dist/development/client/client-vendor.js`
);
// TODO(#112): Enable failing test
// const clientMainVendorMap = path.resolve(
// dir,
// `.fusion/dist/development/client/client-vendor.js.map`
// );
await run(`build --dir=${dir}`);
const clientMainVendorMap = path.resolve(
dir,
`.fusion/dist/development/client/client-vendor.js.map`
);
await cmd(`build --dir=${dir}`);
t.ok(fs.existsSync(serverEntryPath), 'Server Entry file gets compiled');
t.ok(
fs.existsSync(serverMapPath),
Expand All @@ -44,15 +42,14 @@ test('`fusion build` works', async t => {
'Client Entry file sourcemap gets compiled'
);
t.ok(fs.existsSync(clientMainVendor), 'Client vendor file gets compiled');
// TODO(#112): Enable failing test
// t.ok(
// fs.existsSync(clientMainVendorMap),
// 'Client vendor file sourcemap gets compiled'
// );
t.ok(
fs.existsSync(clientMainVendorMap),
'Client vendor file sourcemap gets compiled'
);
t.end();
});

test('`fusion build` works in production', async t => {
test('`fusion build` works in production with a CDN_URL', async t => {
const dir = path.resolve(__dirname, '../fixtures/noop');
const serverEntryPath = path.resolve(
dir,
Expand All @@ -62,40 +59,74 @@ test('`fusion build` works in production', async t => {
dir,
`.fusion/dist/production/server/server-main.js.map`
);
const clientMain = path.resolve(
await cmd(`build --dir=${dir} --production`);
const clientFiles = fs.readdirSync(
path.resolve(dir, '.fusion/dist/production/client')
);
t.ok(
clientFiles.some(f => /client-main-(.*?).js$/.test(f)),
'includes a versioned client-main.js file'
);
t.ok(
clientFiles.some(f => /client-vendor-(.*?).js$/.test(f)),
'includes a versioned client-vendor.js file'
);
t.ok(fs.existsSync(serverEntryPath), 'Server Entry file gets compiled');
t.ok(
fs.existsSync(serverMapPath),
'Server Entry file sourcemap gets compiled'
);
const {res, proc} = await start(`--dir=${dir}`, {
env: Object.assign({}, process.env, {CDN_URL: 'https://cdn.com/test'}),
});
t.ok(
res.includes('src="https://cdn.com/test/client-main'),
'includes a script reference to client-main'
);
t.ok(
res.includes('src="https://cdn.com/test/client-vendor'),
'includes a script reference to client-vendor'
);
proc.kill();
t.end();
});

test('`fusion build` works in production', async t => {
const dir = path.resolve(__dirname, '../fixtures/noop');
const serverEntryPath = path.resolve(
dir,
`.fusion/dist/production/client/client-main-b146db6e5d21f0eee531.js`
`.fusion/dist/production/server/server-main.js`
);
const clientMainMap = path.resolve(
const serverMapPath = path.resolve(
dir,
`.fusion/dist/production/client/client-main-b146db6e5d21f0eee531.js.map`
);
// TODO(#112): Enable failing test
// const clientMainVendor = path.resolve(
// dir,
// `.fusion/dist/production/client/client-vendor-75c3b5ea4d2e744ae2ad.js`
// );
// const clientMainVendorMap = path.resolve(
// dir,
// `.fusion/dist/production/client/client-vendor-75c3b5ea4d2e744ae2ad.js.map`
// );
// const port = await getPort();
await run(`build --dir=${dir} --production`);
`.fusion/dist/production/server/server-main.js.map`
);
await cmd(`build --dir=${dir} --production`);
const clientFiles = fs.readdirSync(
path.resolve(dir, '.fusion/dist/production/client')
);
t.ok(
clientFiles.some(f => /client-main-(.*?).js$/.test(f)),
'includes a versioned client-main.js file'
);
t.ok(
clientFiles.some(f => /client-vendor-(.*?).js$/.test(f)),
'includes a versioned client-vendor.js file'
);
t.ok(fs.existsSync(serverEntryPath), 'Server Entry file gets compiled');
t.ok(
fs.existsSync(serverMapPath),
'Server Entry file sourcemap gets compiled'
);
t.ok(fs.existsSync(clientMain), 'Client Entry file gets compiled');
const {res, proc} = await start(`--dir=${dir}`);
t.ok(
fs.existsSync(clientMainMap),
'Client Entry file sourcemap gets compiled'
res.includes('src="/_static/client-main'),
'includes a script reference to client-main'
);
t.ok(
res.includes('src="/_static/client-vendor'),
'includes a script reference to client-vendor'
);
// TODO(#112): Enable failing test
// t.ok(fs.existsSync(clientMainVendor), 'Client vendor file gets compiled');
// t.ok(
// fs.existsSync(clientMainVendorMap),
// 'Client vendor file sourcemap gets compiled'
// );
proc.kill();
t.end();
});
56 changes: 12 additions & 44 deletions test/cli/dev.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
/* eslint-env node */

const child_process = require('child_process');
const fs = require('fs');
const path = require('path');
const test = require('tape');
const getPort = require('get-port');

const {run} = require('../../bin/cli-runner');
const runnerPath = require.resolve('../../bin/cli-runner');
const {dev} = require('../run-command');

test('`fusion dev` works', async t => {
const dir = path.resolve(__dirname, '../fixtures/noop');
const entryPath = `.fusion/dist/development/server/server-main.js`;
const entry = path.resolve(dir, entryPath);

const {stop} = await run(
`dev --dir=${dir} --no-open --port=${await getPort()}`
);
stop();
const {proc} = await dev(`--dir=${dir}`);
t.ok(fs.existsSync(entry), 'Entry file gets compiled');
proc.kill();
t.end();
});

Expand All @@ -30,9 +24,7 @@ test('`fusion dev` works with assets', async t => {
);
const testFilePath = path.resolve(dir, '.fusion/test-asset');

const {stop} = await run(
`dev --dir=${dir} --no-open --port=${await getPort()}`
);
const {proc} = await dev(`--dir=${dir}`);
t.ok(fs.existsSync(testFilePath), 'Generates test file');
t.ok(fs.existsSync(entryPath), 'Entry file gets compiled');
const assetPath = fs.readFileSync(testFilePath);
Expand All @@ -41,7 +33,7 @@ test('`fusion dev` works with assets', async t => {
'/_static/d41d8cd98f00b204e9800998ecf8427e.js',
'sets correct asset path'
);
stop();
proc.kill();
t.end();
});

Expand All @@ -52,10 +44,9 @@ test('`fusion dev` works with assets with cdnUrl', async t => {
'.fusion/dist/development/server/server-main.js'
);
const testFilePath = path.resolve(dir, '.fusion/test-asset');
process.env.CDN_URL = 'https://cdn.com';
const {stop} = await run(
`dev --dir=${dir} --no-open --port=${await getPort()}`
);
const {proc} = await dev(`--dir=${dir}`, {
env: Object.assign({}, process.env, {CDN_URL: 'https://cdn.com'}),
});
t.ok(fs.existsSync(testFilePath), 'Generates test file');
t.ok(fs.existsSync(entryPath), 'Entry file gets compiled');
const assetPath = fs.readFileSync(testFilePath);
Expand All @@ -64,8 +55,7 @@ test('`fusion dev` works with assets with cdnUrl', async t => {
'https://cdn.com/d41d8cd98f00b204e9800998ecf8427e.js',
'sets correct asset path'
);
stop();
process.env.CDN_URL = '';
proc.kill();
t.end();
});

Expand All @@ -74,30 +64,8 @@ test('`fusion dev` top-level error', async t => {
__dirname,
'../fixtures/server-error-route-component'
);
const port = await getPort();
const command = `require('${runnerPath}').run('dev --dir=${dir} --no-open --port=${port}')`;
const childServer = child_process.spawn('node', ['-e', command]);

function checkContentForError() {
try {
const cmd = `curl -s -H 'Accept: text/html' http://localhost:${port}`;
const result = child_process.execSync(cmd);
return String(result).includes('top-level-route-error');
} catch (e) {
return false;
}
}

function pause(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

let hasError = false;
while (!hasError) {
await pause(200);
hasError = checkContentForError();
}

childServer.kill();
const {res, proc} = await dev(`--dir=${dir}`);
t.ok(res.includes('top-level-route-error'));
proc.kill();
t.end();
});
18 changes: 6 additions & 12 deletions test/cli/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@

const path = require('path');
const test = require('tape');

const run = require('../run-command');

const runnerPath = require.resolve('../../bin/cli-runner');
const {cmd} = require('../run-command');

test('"fusion test" exits w/ unhandled promise rejection in server tests', async t => {
const dir = path.resolve(
__dirname,
'../fixtures/unhandled-promise-rejection-server'
);
const args = `test --dir=${dir}`;

const cmd = `require('${runnerPath}').run('${args}')`;
try {
await run(cmd, {
await cmd(`test --dir=${dir}`, {
env: Object.assign({}, process.env, {
NODE_ENV: 'production',
}),
stdio: 'pipe',
});
t.fail('should not succeed');
} catch (e) {
Expand All @@ -35,11 +30,10 @@ test('`fusion test` exits w/ unhandled promise rejection in browser tests', asyn
__dirname,
'../fixtures/unhandled-promise-rejection-browser'
);
const args = `test --dir=${dir}`;

const cmd = `require('${runnerPath}').run('${args}')`;
try {
await run(cmd);
await cmd(`test --dir=${dir}`, {
stdio: 'pipe',
});
t.fail('should not succeed');
} catch (e) {
t.notEqual(e.code, 0, 'exits with non-zero status code');
Expand Down
Loading

0 comments on commit 9580cc0

Please sign in to comment.