Enable browser tests with npm test (#108)

* Enable browser tests with npm test

* Add chrome-unit script

* Add sudo: true to .travis.yml to support chrome testing

* Add back a safe amount of parallelism

* Fix bug in testserver

* Run node tests in script

* chrome-unit -> unit
This commit is contained in:
Rikki Gibson 2018-05-30 14:01:03 -07:00 коммит произвёл GitHub
Родитель cbf8dad4b4
Коммит ce09bc0435
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 78 добавлений и 30 удалений

64
.scripts/unit.js Normal file
Просмотреть файл

@ -0,0 +1,64 @@
const { major } = require("semver");
const { spawn, exec } = require("child_process");
const { join } = require("path");
const webpackDevServer = spawn(join(__dirname, "../node_modules/.bin/ts-node"), [join(__dirname, "../testserver")], { shell: true })
function cleanupDevServer() {
webpackDevServer.stderr.destroy();
webpackDevServer.stdout.destroy();
webpackDevServer.kill();
};
let mochaRunning = false
const webpackDevServerHandler = (data) => {
if (!mochaRunning) {
mochaRunning = true
const mochaChromePromise = new Promise((resolve, reject) => {
if (major(process.version) < 8) {
// Skip browser tests in pre-node 8
resolve();
} else {
const mochaChromeExecutable = join(__dirname, "../node_modules/.bin/mocha-chrome");
exec(`${mochaChromeExecutable} http://localhost:3001`, (err, stdout, stderr) => {
if (err) {
reject(err);
} else {
console.log(stdout);
console.error(stderr);
resolve();
}
});
}
});
const mochaPromise = new Promise((resolve, reject) => {
const mochaExecutable = join(__dirname, "../node_modules/.bin/_mocha");
const mocha = exec(mochaExecutable, (err, stdout, stderr) => {
if (err) {
reject(err);
} else {
console.log(stdout);
console.error(stderr);
resolve();
}
});
});
Promise.all([mochaPromise, mochaChromePromise]).then(res => {
cleanupDevServer();
process.exit(0);
}).catch((err) => {
console.error(err);
cleanupDevServer();
process.exit(1);
});
}
}
webpackDevServer.stderr.on('data', data => {
console.error("webpack dev server error:", data.toString());
});
webpackDevServer.stdout.on('data', webpackDevServerHandler);
webpackDevServer.on('exit', webpackDevServerHandler);

Просмотреть файл

@ -1,5 +1,7 @@
language: node_js language: node_js
sudo: false sudo: true
addons:
chrome: stable
node_js: node_js:
- "6" - "6"
- "8" - "8"

Просмотреть файл

@ -55,6 +55,7 @@
"mocha": "^5.1.1", "mocha": "^5.1.1",
"mocha-chrome": "^1.1.0", "mocha-chrome": "^1.1.0",
"npm-run-all": "^4.1.2", "npm-run-all": "^4.1.2",
"semver": "^5.5.0",
"should": "5.2.0", "should": "5.2.0",
"shx": "^0.2.2", "shx": "^0.2.2",
"ts-loader": "^4.2.0", "ts-loader": "^4.2.0",
@ -81,16 +82,14 @@
"build:browser": "webpack && node node_modules/uglify-es/bin/uglifyjs --source-map -c -m -o msRestBundle.min.js msRestBundle.js", "build:browser": "webpack && node node_modules/uglify-es/bin/uglifyjs --source-map -c -m -o msRestBundle.min.js msRestBundle.js",
"watch:node": "tsc -w", "watch:node": "tsc -w",
"watch:browser": "webpack -w", "watch:browser": "webpack -w",
"test": "run-p test:tslint test:nodejs-unit", "test": "run-p test:tslint test:unit",
"test:server": "ts-node testserver",
"test:tslint": "tslint -p . -c tslint.json --exclude \"./test/**/*.ts\"", "test:tslint": "tslint -p . -c tslint.json --exclude \"./test/**/*.ts\"",
"test:nodejs-unit": "mocha", "test:unit": "node ./.scripts/unit",
"test:mocha-chrome": "sleep 0.5 && mocha-chrome http://localhost:3001", "test:server": "ts-node testserver",
"test:chrome-unit": "run-p -r test:server test:mocha-chrome",
"prepare": "npm run build", "prepare": "npm run build",
"publish-preview": "npm test && shx rm -rf dist/test && node ./.scripts/publish", "publish-preview": "npm test && shx rm -rf dist/test && node ./.scripts/publish",
"local": "node ./.scripts/local.js", "local": "node ./.scripts/local.js",
"preview": "node ./.scripts/preview.js", "preview": "node ./.scripts/preview.js",
"latest": "node ./.scripts/latest.js" "latest": "node ./.scripts/latest.js"
} }
} }

Просмотреть файл

@ -97,19 +97,11 @@ describe("axiosHttpClient", () => {
}); });
it("should allow canceling requests", async function() { it("should allow canceling requests", async function() {
// ensure that a large upload is actually cancelled
this.timeout(2000);
const controller = getAbortController(); const controller = getAbortController();
const request = new WebResource(`${baseURL}/fileupload`, "POST", new Uint8Array(1024*1024*100), undefined, undefined, true, controller.signal); const request = new WebResource(`${baseURL}/fileupload`, "POST", new Uint8Array(1024*1024*10), undefined, undefined, true, controller.signal);
const client = new AxiosHttpClient(); const client = new AxiosHttpClient();
const promise = client.sendRequest(request); const promise = client.sendRequest(request);
await new Promise((resolve) => { controller.abort();
setTimeout(() => {
controller.abort();
resolve();
});
});
try { try {
await promise; await promise;
assert.fail(''); assert.fail('');
@ -139,23 +131,15 @@ describe("axiosHttpClient", () => {
}); });
it("should allow canceling multiple requests with one token", async function () { it("should allow canceling multiple requests with one token", async function () {
// ensure that a large upload is actually cancelled
this.timeout(4000);
const controller = getAbortController(); const controller = getAbortController();
const buf = new Uint8Array(1024*1024*100); const buf = new Uint8Array(1024*1024*1);
const requests = [ const requests = [
new WebResource(`${baseURL}/fileupload`, "POST", buf, undefined, undefined, true, controller.signal), new WebResource(`${baseURL}/fileupload`, "POST", buf, undefined, undefined, true, controller.signal),
new WebResource(`${baseURL}/fileupload`, "POST", buf, undefined, undefined, true, controller.signal) new WebResource(`${baseURL}/fileupload`, "POST", buf, undefined, undefined, true, controller.signal)
]; ];
const client = new AxiosHttpClient(); const client = new AxiosHttpClient();
const promises = requests.map(r => client.sendRequest(r)); const promises = requests.map(r => client.sendRequest(r));
await new Promise((resolve) => { controller.abort();
setTimeout(() => {
controller.abort();
resolve();
});
});
// Ensure each promise is individually rejected // Ensure each promise is individually rejected
for (const promise of promises) { for (const promise of promises) {
try { try {

Просмотреть файл

@ -17,18 +17,17 @@ app.use(express.static(path.join(__dirname, "../")));
app.use(express.static(path.join(__dirname, "../test/resources/"))); app.use(express.static(path.join(__dirname, "../test/resources/")));
app.post("/fileupload", function(req, res) { app.post("/fileupload", function(req, res) {
res.status(200);
req.pipe(res); req.pipe(res);
}); });
app.get("/set-cookie", function(req, res) { app.get("/set-cookie", function(req, res) {
res.setHeader("Set-Cookie", "data=123456"); res.setHeader("Set-Cookie", "data=123456");
res.end(200); res.end();
}); });
app.get("/cookie", function(req, res) { app.get("/cookie", function(req, res) {
res.setHeader("Cookie", req.header("Cookie")); res.setHeader("Cookie", req.header("Cookie"));
res.end(200); res.end();
}); });
app.listen(port, function() { app.listen(port, function() {