Skip to content

Commit 16f9c46

Browse files
ctavanSimenB
andauthored
fix: add Jest/jsdom compatibility (#642)
Jest runs browser tests in a jsdom environment which now also supports web crypto polyfills. Since ESM support in Jest is currently still limited, it requires a commonJS build for browser environments, see the discussion in #616 for all the details. Co-authored-by: Christoph Tavan <[email protected]> Co-authored-by: Simen Bekkhus <[email protected]>
1 parent 04686f5 commit 16f9c46

13 files changed

+7388
-55
lines changed

babel.config.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
"presets": [],
33
"plugins": [],
44
"env": {
5-
"commonjs": {
5+
"commonjsNode": {
66
"presets": [["@babel/preset-env", { "targets": { "node": "10" }, "modules": "commonjs" }]]
77
},
8-
"esmBrowser": {
9-
"presets": [["@babel/preset-env", { "modules": false }]]
10-
},
118
"esmNode": {
129
"presets": [["@babel/preset-env", { "targets": { "node": "10" }, "modules": false }]]
10+
},
11+
"commonjsBrowser": {
12+
"presets": [["@babel/preset-env", { "modules": "commonjs" }]]
13+
},
14+
"esmBrowser": {
15+
"presets": [["@babel/preset-env", { "modules": false }]]
1316
}
1417
}
1518
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>UUID esmodule webpack example</title>
5+
</head>
6+
<body>
7+
<script type="text/javascript" src="./dist/allRequire.js"></script>
8+
</body>
9+
</html>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const uuid = require('uuid');
2+
const {
3+
NIL: NIL_UUID,
4+
parse: uuidParse,
5+
stringify: uuidStringify,
6+
v1: uuidv1,
7+
v3: uuidv3,
8+
v4: uuidv4,
9+
v5: uuidv5,
10+
validate: uuidValidate,
11+
version: uuidVersion,
12+
} = uuid;
13+
14+
const { default: testpage } = require('../utils/testpage');
15+
16+
testpage(function (addTest, done) {
17+
addTest('Named exports');
18+
19+
addTest('uuidv1()', uuidv1());
20+
21+
addTest('uuidv4()', uuidv4());
22+
23+
// ... using predefined DNS namespace (for domain names)
24+
addTest('uuidv3() DNS', uuidv3('hello.example.com', uuidv3.DNS));
25+
26+
// ... using predefined URL namespace (for, well, URLs)
27+
addTest('uuidv3() URL', uuidv3('http://example.com/hello', uuidv3.URL));
28+
29+
// ... using a custom namespace
30+
//
31+
// Note: Custom namespaces should be a UUID string specific to your application!
32+
// E.g. the one here was generated using this modules `uuid` CLI.
33+
const MY_NAMESPACE = '55238d15-c926-4598-b49d-cf4e913ba13c';
34+
addTest('uuidv3() MY_NAMESPACE', uuidv3('Hello, World!', MY_NAMESPACE));
35+
36+
// ... using predefined DNS namespace (for domain names)
37+
addTest('uuidv5() DNS', uuidv5('hello.example.com', uuidv5.DNS));
38+
39+
// ... using predefined URL namespace (for, well, URLs)
40+
addTest('uuidv5() URL', uuidv5('http://example.com/hello', uuidv5.URL));
41+
42+
// ... using a custom namespace
43+
//
44+
// Note: Custom namespaces should be a UUID string specific to your application!
45+
// E.g. the one here was generated using this modules `uuid` CLI.
46+
// const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
47+
addTest('uuidv5() MY_NAMESPACE', uuidv5('Hello, World!', MY_NAMESPACE));
48+
49+
// Utility functions
50+
addTest('NIL_UUID', NIL_UUID);
51+
addTest('uuidParse()', uuidParse(MY_NAMESPACE));
52+
addTest('uuidStringify()', uuidStringify(uuidParse(MY_NAMESPACE)));
53+
addTest('uuidValidate()', uuidValidate(MY_NAMESPACE));
54+
addTest('uuidVersion()', uuidVersion(MY_NAMESPACE));
55+
56+
addTest('Default export');
57+
58+
addTest('uuid.v1()', uuid.v1());
59+
addTest('uuid.v4()', uuid.v4());
60+
addTest('uuid.v3() DNS', uuid.v3('hello.example.com', uuid.v3.DNS));
61+
addTest('uuid.v3() URL', uuid.v3('http://example.com/hello', uuid.v3.URL));
62+
addTest('uuid.v3() MY_NAMESPACE', uuid.v3('Hello, World!', MY_NAMESPACE));
63+
addTest('uuid.v5() DNS', uuid.v5('hello.example.com', uuid.v5.DNS));
64+
addTest('uuid.v5() URL', uuid.v5('http://example.com/hello', uuid.v5.URL));
65+
addTest('uuid.v5() MY_NAMESPACE', uuid.v5('Hello, World!', MY_NAMESPACE));
66+
67+
addTest('uuid.NIL', uuid.NIL);
68+
addTest('uuid.parse()', uuid.parse(MY_NAMESPACE));
69+
addTest('uuid.stringify()', uuid.stringify(uuid.parse(MY_NAMESPACE)));
70+
addTest('uuid.validate()', uuid.validate(MY_NAMESPACE));
71+
addTest('uuid.version()', uuid.version(MY_NAMESPACE));
72+
73+
done();
74+
});

examples/browser-webpack/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = {
44
},
55
entry: {
66
all: './example-all.js',
7+
allRequire: './example-all-require.js',
78
v1: './example-v1.js',
89
v4: './example-v4.js',
910

examples/node-commonjs/package-lock.json

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

examples/node-esmodules/package-lock.json

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

examples/node-jest/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# uuid example Node.js Jest
2+
3+
```
4+
npm install
5+
npm test
6+
```

examples/node-jest/jsdom.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** @jest-environment jsdom */
2+
3+
const uuid = require('uuid');
4+
5+
test('uuidv4()', () => {
6+
const val = uuid.v4();
7+
expect(uuid.version(val)).toBe(4);
8+
});

examples/node-jest/node.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const uuid = require('uuid');
2+
3+
test('uuidv4()', () => {
4+
const val = uuid.v4();
5+
expect(uuid.version(val)).toBe(4);
6+
});

0 commit comments

Comments
 (0)