Harden tests (...a bit)

This commit is contained in:
mrkishi
2019-05-08 00:57:59 -03:00
parent 8bdd363a19
commit e6c1a54164
35 changed files with 726 additions and 685 deletions

View File

@@ -2,15 +2,14 @@ import sirv from 'sirv';
import polka from 'polka';
import * as sapper from '@sapper/server';
const { PORT, NODE_ENV } = process.env;
const dev = NODE_ENV === 'development';
import { start, dev } from '../../common.js';
polka()
const app = polka()
.use(
'custom-basepath',
sirv('static', { dev }),
sapper.middleware()
)
.listen(PORT, err => {
if (err) console.log('error', err);
});
);
start(app);

View File

@@ -1,51 +1,36 @@
import * as assert from 'assert';
import * as puppeteer from 'puppeteer';
import * as api from '../../../api';
import { walk } from '../../utils';
import { AppRunner } from '../AppRunner';
import { wait } from '../../utils';
describe('with-basepath', function() {
this.timeout(10000);
let runner: AppRunner;
let page: puppeteer.Page;
let base: string;
// helpers
let start: () => Promise<void>;
let prefetchRoutes: () => Promise<void>;
let title: () => Promise<string>;
let r: AppRunner;
// hooks
before(async () => {
await api.build({ cwd: __dirname });
await api.export({
cwd: __dirname,
basepath: '/custom-basepath'
});
runner = new AppRunner(__dirname, '__sapper__/build/server/server.js');
({ base, start, page, prefetchRoutes, title } = await runner.start());
before('build app', () => api.build({ cwd: __dirname }));
before('export app', () => api.export({ cwd: __dirname, basepath: '/custom-basepath' }));
before('start runner', async () => {
r = await new AppRunner().start(__dirname);
});
after(() => runner.end());
after(() => r && r.end());
// tests
it('serves /custom-basepath', async () => {
await page.goto(`${base}/custom-basepath`);
await r.load('/custom-basepath');
assert.equal(
await page.$eval('h1', node => node.textContent),
await r.text('h1'),
'Great success!'
);
});
it('emits a basepath message', async () => {
await page.goto(`${base}/custom-basepath`);
await r.load('/custom-basepath');
assert.deepEqual(runner.messages, [{
assert.deepEqual(r.messages, [{
__sapper__: true,
event: 'basepath',
basepath: '/custom-basepath'
@@ -71,35 +56,39 @@ describe('with-basepath', function() {
});
it('redirects on server', async () => {
await page.goto(`${base}/custom-basepath/redirect-from`);
await r.load('/custom-basepath/redirect-from');
assert.equal(
page.url(),
`${base}/custom-basepath/redirect-to`
r.page.url(),
`${r.base}/custom-basepath/redirect-to`
);
assert.equal(
await title(),
await r.text('h1'),
'redirected'
);
});
it('redirects in client', async () => {
await page.goto(`${base}/custom-basepath`);
await start();
await prefetchRoutes();
await r.load('/custom-basepath');
await r.sapper.start();
await r.sapper.prefetchRoutes();
await page.click('[href="redirect-from"]');
await wait(50);
await r.page.click('[href="redirect-from"]');
await r.wait();
assert.equal(
page.url(),
`${base}/custom-basepath/redirect-to`
r.page.url(),
`${r.base}/custom-basepath/redirect-to`
);
assert.equal(
await title(),
await r.text('h1'),
'redirected'
);
});
});
it('survives the tests with no server errors', () => {
assert.deepEqual(r.errors, []);
});
});