Skip to content

Commit 3cb8203

Browse files
committed
Add BlobWorker tests
1 parent b1178fc commit 3cb8203

9 files changed

+202
-26
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ dist/*
77
dist-*/*
88
node_modules/
99
test/rollup/dist/
10-
test/webpack/dist*
10+
test/webpack/dist/
1111
test/workers/*.js
1212
.DS_Store
1313
Thumbs.db

package-lock.json

Lines changed: 100 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"test": "run-s test:ava test:puppeteer:basic test:puppeteer:webpack",
2121
"test:ava": "cross-env TS_NODE_FILES=true ava",
2222
"test:puppeteer:basic": "puppet-run --plugin=mocha --bundle=./test/workers/:/workers/ --serve=./bundle/worker.js:/worker.js ./test/*.chromium*.ts",
23-
"test:puppeteer:webpack": "puppet-run --serve ./test/webpack/dist.web/0.worker.js --serve ./test/webpack/dist.web/1.worker.js --plugin=mocha ./test/webpack/webpack.chromium.mocha.ts",
23+
"test:puppeteer:webpack": "puppet-run --serve ./test/webpack/dist/app.web/0.worker.js --serve ./test/webpack/dist/app.web/1.worker.js --plugin=mocha ./test/webpack/webpack.chromium.mocha.ts",
2424
"posttest": "tslint --project .",
2525
"prepare": "run-s build"
2626
},
@@ -71,17 +71,18 @@
7171
"npm-run-all": "^4.1.5",
7272
"puppet-run": "^0.11.1",
7373
"puppet-run-plugin-mocha": "^0.10.0-alpha",
74+
"raw-loader": "^4.0.1",
7475
"rimraf": "^2.6.3",
7576
"rollup": "^1.16.2",
7677
"rollup-plugin-commonjs": "^10.0.1",
7778
"rollup-plugin-node-resolve": "^5.1.0",
7879
"threads-plugin": "^1.2.0",
7980
"tiny-worker": "^2.2.0",
8081
"ts-loader": "^6.0.1",
81-
"ts-node": "^8.1.0",
82+
"ts-node": "^8.10.2",
8283
"tslint": "^5.16.0",
8384
"tslint-config-prettier": "^1.18.0",
84-
"typescript": "^3.9.3",
85+
"typescript": "^3.9.5",
8586
"wavy": "^1.0.4",
8687
"webpack": "^4.32.2",
8788
"worker-plugin": "^3.1.0"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference types="./raw-loader" />
2+
3+
import { spawn, BlobWorker } from "../../src/index"
4+
import AdditionWorkerNodeBundle from "raw-loader!./dist/addition-worker.node/worker.js"
5+
import AdditionWorkerWebBundle from "raw-loader!./dist/addition-worker.web/worker.js"
6+
7+
const AdditionWorkerBundle = (process as any).browser ? AdditionWorkerWebBundle : AdditionWorkerNodeBundle
8+
type AdditionWorker = (a: number, b: number) => number
9+
10+
async function test() {
11+
// We also want to test if referencing multiple different workers in a module
12+
// built using webpack works
13+
14+
const add = await spawn<AdditionWorker>(BlobWorker.fromText(AdditionWorkerBundle))
15+
const result = await add(2, 3)
16+
17+
if (result !== 5) {
18+
throw Error("Unexpected result returned by addition worker: " + result)
19+
}
20+
21+
return "test succeeded"
22+
}
23+
24+
export default test

test/webpack/raw-loader.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module "raw-loader!*" {
2+
const content: string
3+
export = content
4+
}
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
// NOTE:
2-
// We are gonna test the bundle that previously been built by the AVA tests (see webpack.test.ts)
2+
// We are gonna test the bundles previously built by the AVA tests (see webpack.test.ts)
33

44
describe("threads webpack browser bundle", function() {
55
this.timeout(8000)
66

77
it("works fine", async function() {
8-
const bundle = require("./dist.web/main")
8+
const bundle = require("./dist/app.web/main")
9+
await bundle.test()
10+
})
11+
})
12+
13+
describe("threads webpack browser bundle with inlined worker", function() {
14+
this.timeout(8000)
15+
16+
it("works fine", async function() {
17+
const bundle = require("./dist/app-inlined.web/main")
918
await bundle.test()
1019
})
1120
})

test/webpack/webpack.node.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = {
1313
library: "test",
1414
libraryExport: "default",
1515
libraryTarget: "commonjs",
16-
path: path.resolve(__dirname, "./dist.node")
16+
path: path.resolve(__dirname, "./dist/app.node")
1717
},
1818
module: {
1919
rules: [

test/webpack/webpack.test.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import test from "ava"
2+
import * as path from "path"
23
import Webpack from "webpack"
34

45
const browserConfig = require("./webpack.web.config")
@@ -30,6 +31,60 @@ test("can create a working server bundle with webpack", async t => {
3031
const stats = await runWebpack(serverConfig)
3132
t.deepEqual(stats.compilation.errors, [], stringifyWebpackError(stats.compilation.errors[0]))
3233

33-
const bundle = require("./dist.node/main")
34+
const bundle = require("./dist/app.node/main")
3435
await bundle.test()
3536
})
37+
38+
test("can inline a worker into a browser bundle", async t => {
39+
// Bundle worker
40+
let stats = await runWebpack({
41+
...serverConfig,
42+
entry: require.resolve("./addition-worker"),
43+
output: {
44+
filename: "worker.js",
45+
path: path.resolve(__dirname, "dist/addition-worker.web")
46+
},
47+
target: "webworker"
48+
})
49+
t.deepEqual(stats.compilation.errors, [], stringifyWebpackError(stats.compilation.errors[0]))
50+
51+
// Bundle app
52+
stats = await runWebpack({
53+
...serverConfig,
54+
entry: require.resolve("./app-with-inlined-worker"),
55+
output: {
56+
...serverConfig.output,
57+
path: path.resolve(__dirname, "dist/app-inlined.web")
58+
}
59+
})
60+
t.deepEqual(stats.compilation.errors, [], stringifyWebpackError(stats.compilation.errors[0]))
61+
})
62+
63+
test("can inline a worker into a server bundle", async t => {
64+
// Bundle server worker
65+
let stats = await runWebpack({
66+
...serverConfig,
67+
entry: require.resolve("./addition-worker"),
68+
output: {
69+
filename: "worker.js",
70+
path: path.resolve(__dirname, "dist/addition-worker.node")
71+
}
72+
})
73+
t.deepEqual(stats.compilation.errors, [], stringifyWebpackError(stats.compilation.errors[0]))
74+
75+
// Bundle app
76+
stats = await runWebpack({
77+
...serverConfig,
78+
entry: require.resolve("./app-with-inlined-worker"),
79+
output: {
80+
...serverConfig.output,
81+
path: path.resolve(__dirname, "dist/app-inlined.node")
82+
}
83+
})
84+
t.deepEqual(stats.compilation.errors, [], stringifyWebpackError(stats.compilation.errors[0]))
85+
86+
const bundle = require("./dist/app-inlined.node/main")
87+
const result = await bundle.test()
88+
89+
t.is(result, "test succeeded")
90+
})

test/webpack/webpack.web.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = {
1010
library: "test",
1111
libraryExport: "default",
1212
libraryTarget: "commonjs",
13-
path: path.resolve(__dirname, "./dist.web")
13+
path: path.resolve(__dirname, "./dist/app.web")
1414
},
1515
module: {
1616
rules: [

0 commit comments

Comments
 (0)