allow to run mocha tests in parallel (#880)

This commit is contained in:
Konstantin Vyatkin 2020-06-13 05:34:59 -04:00 committed by GitHub
parent 96431ed4a1
commit 2751bd56eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 26 deletions

View File

@ -1,5 +1,8 @@
import {Headers} from '../src/index.js'; import {Headers} from '../src/index.js';
import chai from 'chai'; import chai from 'chai';
import chaiIterator from 'chai-iterator';
chai.use(chaiIterator);
const {expect} = chai; const {expect} = chai;

View File

@ -48,17 +48,6 @@ chai.use(chaiString);
chai.use(chaiTimeout); chai.use(chaiTimeout);
const {expect} = chai; const {expect} = chai;
const local = new TestServer();
const base = `http://${local.hostname}:${local.port}/`;
before(done => {
local.start(done);
});
after(done => {
local.stop(done);
});
function streamToPromise(stream, dataHandler) { function streamToPromise(stream, dataHandler) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
stream.on('data', (...args) => { stream.on('data', (...args) => {
@ -72,6 +61,18 @@ function streamToPromise(stream, dataHandler) {
} }
describe('node-fetch', () => { describe('node-fetch', () => {
const local = new TestServer();
let base;
before(async () => {
await local.start();
base = `http://${local.hostname}:${local.port}/`;
});
after(async () => {
return local.stop();
});
it('should return a promise', () => { it('should return a promise', () => {
const url = `${base}hello`; const url = `${base}hello`;
const p = fetch(url); const p = fetch(url);
@ -2124,9 +2125,9 @@ describe('node-fetch', () => {
expect(extractContentType(null)).to.be.null; expect(extractContentType(null)).to.be.null;
}); });
it('should encode URLs as UTF-8', () => { it('should encode URLs as UTF-8', async () => {
const url = `${base}möbius`; const url = `${base}möbius`;
const res = await fetch(url);
fetch(url).then(res => expect(res.url).to.equal(`${base}m%C3%B6bius`)); expect(res.url).to.equal(`${base}m%C3%B6bius`);
}); });
}); });

View File

@ -13,10 +13,19 @@ import {Request} from '../src/index.js';
const {expect} = chai; const {expect} = chai;
const local = new TestServer();
const base = `http://${local.hostname}:${local.port}/`;
describe('Request', () => { describe('Request', () => {
const local = new TestServer();
let base;
before(async () => {
await local.start();
base = `http://${local.hostname}:${local.port}/`;
});
after(async () => {
return local.stop();
});
it('should have attributes conforming to Web IDL', () => { it('should have attributes conforming to Web IDL', () => {
const request = new Request('https://github.com/'); const request = new Request('https://github.com/');
const enumerableProperties = []; const enumerableProperties = [];

View File

@ -8,10 +8,19 @@ import TestServer from './utils/server.js';
const {expect} = chai; const {expect} = chai;
const local = new TestServer();
const base = `http://${local.hostname}:${local.port}/`;
describe('Response', () => { describe('Response', () => {
const local = new TestServer();
let base;
before(async () => {
await local.start();
base = `http://${local.hostname}:${local.port}/`;
});
after(async () => {
return local.stop();
});
it('should have attributes conforming to Web IDL', () => { it('should have attributes conforming to Web IDL', () => {
const res = new Response(); const res = new Response();
const enumerableProperties = []; const enumerableProperties = [];

View File

@ -1,12 +1,11 @@
import http from 'http'; import http from 'http';
import zlib from 'zlib'; import zlib from 'zlib';
import Busboy from 'busboy'; import Busboy from 'busboy';
import {once} from 'events';
export default class TestServer { export default class TestServer {
constructor() { constructor() {
this.server = http.createServer(this.router); this.server = http.createServer(this.router);
this.port = 30001;
this.hostname = 'localhost';
// Node 8 default keepalive timeout is 5000ms // Node 8 default keepalive timeout is 5000ms
// make it shorter here as we want to close server quickly at the end of tests // make it shorter here as we want to close server quickly at the end of tests
this.server.keepAliveTimeout = 1000; this.server.keepAliveTimeout = 1000;
@ -18,12 +17,22 @@ export default class TestServer {
}); });
} }
start(cb) { async start() {
this.server.listen(this.port, this.hostname, cb); this.server.listen(0, 'localhost');
return once(this.server, 'listening');
} }
stop(cb) { async stop() {
this.server.close(cb); this.server.close();
return once(this.server, 'close');
}
get port() {
return this.server.address().port;
}
get hostname() {
return 'localhost';
} }
mockResponse(responseHandler) { mockResponse(responseHandler) {