Remove async/await from test.js

This commit is contained in:
Emil Tholin
2018-01-07 12:22:40 +01:00
parent 2b3472b1b1
commit 98e904dcfc

View File

@@ -62,21 +62,27 @@ function run(env) {
}); });
} }
before(async () => { before(() => {
process.chdir(path.resolve(__dirname, '../app')); process.chdir(path.resolve(__dirname, '../app'));
process.env.NODE_ENV = env; process.env.NODE_ENV = env;
let exec_promise = Promise.resolve();
let sapper;
if (env === 'production') { if (env === 'production') {
const cli = path.resolve(__dirname, '../../cli/index.js'); const cli = path.resolve(__dirname, '../../cli/index.js');
await exec(`${cli} build`); exec_promise = exec(`${cli} build`);
} }
return exec_promise.then(() => {
const resolved = require.resolve('../..'); const resolved = require.resolve('../..');
delete require.cache[resolved]; delete require.cache[resolved];
const sapper = require(resolved); sapper = require(resolved);
PORT = await getPort(); return getPort();
}).then(port => {
PORT = port;
base = `http://localhost:${PORT}`; base = `http://localhost:${PORT}`;
global.fetch = (url, opts) => { global.fetch = (url, opts) => {
@@ -85,11 +91,12 @@ function run(env) {
}; };
let captured; let captured;
capture = async fn => { capture = fn => {
const result = captured = []; const result = captured = [];
await fn(); return fn().then(() => {
captured = null; captured = null;
return result; return result;
});
}; };
const app = express(); const app = express();
@@ -111,6 +118,7 @@ function run(env) {
}); });
}); });
}); });
});
after(() => { after(() => {
server.close(); server.close();
@@ -137,164 +145,176 @@ function run(env) {
}); });
}); });
afterEach(async () => { afterEach(() => {
await nightmare.end(); return nightmare.end();
}); });
it('serves /', async () => { it('serves /', () => {
const title = await nightmare return nightmare
.goto(base) .goto(base)
.evaluate(() => document.querySelector('h1').textContent); .evaluate(() => document.querySelector('h1').textContent)
.then(title => {
assert.equal(title, 'Great success!'); assert.equal(title, 'Great success!');
}); });
});
it('serves static route', async () => { it('serves static route', () => {
const title = await nightmare return nightmare
.goto(`${base}/about`) .goto(`${base}/about`)
.evaluate(() => document.querySelector('h1').textContent); .evaluate(() => document.querySelector('h1').textContent)
.then(title => {
assert.equal(title, 'About this site'); assert.equal(title, 'About this site');
}); });
});
it('serves dynamic route', async () => { it('serves dynamic route', () => {
const title = await nightmare return nightmare
.goto(`${base}/blog/what-is-sapper`) .goto(`${base}/blog/what-is-sapper`)
.evaluate(() => document.querySelector('h1').textContent); .evaluate(() => document.querySelector('h1').textContent)
.then(title => {
assert.equal(title, 'What is Sapper?'); assert.equal(title, 'What is Sapper?');
}); });
it('navigates to a new page without reloading', async () => {
await nightmare.goto(base).wait(() => window.READY).wait(200);
const requests = await capture(async () => {
await nightmare.click('a[href="/about"]');
}); });
assert.equal( it('navigates to a new page without reloading', () => {
await nightmare.path(), let requests;
'/about' return nightmare
); .goto(base).wait(() => window.READY).wait(200)
.then(() => {
return capture(() => {
return nightmare.click('a[href="/about"]');
});
})
.then(reqs => {
requests = reqs;
assert.equal( return nightmare.path();
await nightmare.evaluate(() => document.title), })
'About' .then(path => {
); assert.equal(path, '/about');
return nightmare.evaluate(() => document.title);
})
.then(title => {
assert.equal(title, 'About');
assert.deepEqual(requests.map(r => r.url), []); assert.deepEqual(requests.map(r => r.url), []);
}); });
});
it('navigates programmatically', async () => { it('navigates programmatically', () => {
await nightmare return nightmare
.goto(`${base}/about`) .goto(`${base}/about`)
.wait(() => window.READY) .wait(() => window.READY)
.click('.goto') .click('.goto')
.wait(() => window.location.pathname === '/blog/what-is-sapper') .wait(() => window.location.pathname === '/blog/what-is-sapper')
.wait(100); .wait(100)
.then(() => nightmare.evaluate(() => document.title))
assert.equal( .then(title => {
await nightmare.evaluate(() => document.title), assert.equal(title, 'What is Sapper?');
'What is Sapper?' });
);
}); });
it('prefetches programmatically', async () => { it('prefetches programmatically', () => {
await nightmare return nightmare
.goto(`${base}/about`) .goto(`${base}/about`)
.wait(() => window.READY); .wait(() => window.READY)
.then(() => {
const requests = await capture(async () => { return capture(() => {
return await nightmare return nightmare
.click('.prefetch') .click('.prefetch')
.wait(100); .wait(100);
}); });
})
.then(requests => {
assert.ok(!!requests.find(r => r.url === '/api/blog/why-the-name')); assert.ok(!!requests.find(r => r.url === '/api/blog/why-the-name'));
}); });
});
it('scrolls to active deeplink', async () => { it('scrolls to active deeplink', () => {
const scrollY = await nightmare return nightmare
.goto(`${base}/blog/a-very-long-post#four`) .goto(`${base}/blog/a-very-long-post#four`)
.wait(() => window.READY) .wait(() => window.READY)
.wait(100) .wait(100)
.evaluate(() => window.scrollY); .evaluate(() => window.scrollY)
.then(scrollY => {
assert.ok(scrollY > 0, scrollY); assert.ok(scrollY > 0, scrollY);
}); });
});
it('reuses prefetch promise', async () => { it('reuses prefetch promise', () => {
await nightmare return nightmare
.goto(`${base}/blog`) .goto(`${base}/blog`)
.wait(() => window.READY) .wait(() => window.READY)
.wait(200); .wait(200)
.then(() => {
const mouseover_requests = (await capture(async () => { return capture(() => {
await nightmare return nightmare
.mouseover('[href="/blog/what-is-sapper"]') .mouseover('[href="/blog/what-is-sapper"]')
.wait(200); .wait(200);
})).map(r => r.url); });
})
assert.deepEqual(mouseover_requests, [ .then(mouseover_requests => {
assert.deepEqual(mouseover_requests.map(r => r.url), [
'/api/blog/what-is-sapper' '/api/blog/what-is-sapper'
]); ]);
const click_requests = (await capture(async () => { return capture(() => {
await nightmare return nightmare
.click('[href="/blog/what-is-sapper"]') .click('[href="/blog/what-is-sapper"]')
.wait(200); .wait(200);
})).map(r => r.url); });
})
assert.deepEqual(click_requests, []); .then(click_requests => {
assert.deepEqual(click_requests.map(r => r.url), []);
});
}); });
it('cancels navigation if subsequent navigation occurs during preload', async () => { it('cancels navigation if subsequent navigation occurs during preload', () => {
await nightmare return nightmare
.goto(base) .goto(base)
.wait(() => window.READY) .wait(() => window.READY)
.click('a[href="/slow-preload"]') .click('a[href="/slow-preload"]')
.wait(100) .wait(100)
.click('a[href="/about"]') .click('a[href="/about"]')
.wait(100); .wait(100)
.then(() => nightmare.path())
.then(path => {
assert.equal(path, '/about');
assert.equal( return nightmare.evaluate(() => document.querySelector('h1').textContent);
await nightmare.path(), })
'/about' .then(header_text => {
); assert.equal(header_text, 'About this site');
assert.equal( return nightmare.evaluate(() => window.fulfil({})).wait(100);
await nightmare.evaluate(() => document.querySelector('h1').textContent), })
'About this site' .then(() => nightmare.path())
); .then(path => {
assert.equal(path, '/about');
await nightmare return nightmare.evaluate(() => document.querySelector('h1').textContent);
.evaluate(() => window.fulfil({})) })
.wait(100); .then(header_text => {
assert.equal(header_text, 'About this site');
assert.equal( return nightmare.evaluate(() => window.fulfil({})).wait(100);
await nightmare.path(), });
'/about'
);
assert.equal(
await nightmare.evaluate(() => document.querySelector('h1').textContent),
'About this site'
);
}); });
it('passes entire request object to preload', async () => { it('passes entire request object to preload', () => {
const html = await nightmare return nightmare
.goto(`${base}/show-url`) .goto(`${base}/show-url`)
.evaluate(() => document.querySelector('p').innerHTML); .evaluate(() => document.querySelector('p').innerHTML)
.then(html => {
assert.equal(html, `URL is /show-url`); assert.equal(html, `URL is /show-url`);
}); });
}); });
});
describe('headers', () => { describe('headers', () => {
it('sets Content-Type and Link...preload headers', async () => { it('sets Content-Type and Link...preload headers', () => {
const { headers } = await get('/'); return get('/').then(({ headers }) => {
assert.equal( assert.equal(
headers['Content-Type'], headers['Content-Type'],
'text/html' 'text/html'
@@ -307,6 +327,7 @@ function run(env) {
}); });
}); });
}); });
});
} }
function exec(cmd) { function exec(cmd) {