Skip to content

Commit

Permalink
fix: add Jest/jsdom compatibility
Browse files Browse the repository at this point in the history
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 <dev@tavan.de>
  • Loading branch information
SimenB and ctavan committed Jul 29, 2022
1 parent 2fa8970 commit 8b6bf8f
Show file tree
Hide file tree
Showing 13 changed files with 7,374 additions and 55 deletions.
11 changes: 7 additions & 4 deletions babel.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
"presets": [],
"plugins": [],
"env": {
"commonjs": {
"commonjsNode": {
"presets": [["@babel/preset-env", { "targets": { "node": "10" }, "modules": "commonjs" }]]
},
"esmBrowser": {
"presets": [["@babel/preset-env", { "modules": false }]]
},
"esmNode": {
"presets": [["@babel/preset-env", { "targets": { "node": "10" }, "modules": false }]]
},
"commonjsBrowser": {
"presets": [["@babel/preset-env", { "modules": "commonjs" }]]
},
"esmBrowser": {
"presets": [["@babel/preset-env", { "modules": false }]]
}
}
}
9 changes: 9 additions & 0 deletions examples/browser-webpack/example-all-require.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>UUID esmodule webpack example</title>
</head>
<body>
<script type="text/javascript" src="./dist/allRequire.js"></script>
</body>
</html>
74 changes: 74 additions & 0 deletions examples/browser-webpack/example-all-require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const uuid = require('uuid');
const {
NIL: NIL_UUID,
parse: uuidParse,
stringify: uuidStringify,
v1: uuidv1,
v3: uuidv3,
v4: uuidv4,
v5: uuidv5,
validate: uuidValidate,
version: uuidVersion,
} = uuid;

const { default: testpage } = require('../utils/testpage');

testpage(function (addTest, done) {
addTest('Named exports');

addTest('uuidv1()', uuidv1());

addTest('uuidv4()', uuidv4());

// ... using predefined DNS namespace (for domain names)
addTest('uuidv3() DNS', uuidv3('hello.example.com', uuidv3.DNS));

// ... using predefined URL namespace (for, well, URLs)
addTest('uuidv3() URL', uuidv3('http://example.com/hello', uuidv3.URL));

// ... using a custom namespace
//
// Note: Custom namespaces should be a UUID string specific to your application!
// E.g. the one here was generated using this modules `uuid` CLI.
const MY_NAMESPACE = '55238d15-c926-4598-b49d-cf4e913ba13c';
addTest('uuidv3() MY_NAMESPACE', uuidv3('Hello, World!', MY_NAMESPACE));

// ... using predefined DNS namespace (for domain names)
addTest('uuidv5() DNS', uuidv5('hello.example.com', uuidv5.DNS));

// ... using predefined URL namespace (for, well, URLs)
addTest('uuidv5() URL', uuidv5('http://example.com/hello', uuidv5.URL));

// ... using a custom namespace
//
// Note: Custom namespaces should be a UUID string specific to your application!
// E.g. the one here was generated using this modules `uuid` CLI.
// const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
addTest('uuidv5() MY_NAMESPACE', uuidv5('Hello, World!', MY_NAMESPACE));

// Utility functions
addTest('NIL_UUID', NIL_UUID);
addTest('uuidParse()', uuidParse(MY_NAMESPACE));
addTest('uuidStringify()', uuidStringify(uuidParse(MY_NAMESPACE)));
addTest('uuidValidate()', uuidValidate(MY_NAMESPACE));
addTest('uuidVersion()', uuidVersion(MY_NAMESPACE));

addTest('Default export');

addTest('uuid.v1()', uuid.v1());
addTest('uuid.v4()', uuid.v4());
addTest('uuid.v3() DNS', uuid.v3('hello.example.com', uuid.v3.DNS));
addTest('uuid.v3() URL', uuid.v3('http://example.com/hello', uuid.v3.URL));
addTest('uuid.v3() MY_NAMESPACE', uuid.v3('Hello, World!', MY_NAMESPACE));
addTest('uuid.v5() DNS', uuid.v5('hello.example.com', uuid.v5.DNS));
addTest('uuid.v5() URL', uuid.v5('http://example.com/hello', uuid.v5.URL));
addTest('uuid.v5() MY_NAMESPACE', uuid.v5('Hello, World!', MY_NAMESPACE));

addTest('uuid.NIL', uuid.NIL);
addTest('uuid.parse()', uuid.parse(MY_NAMESPACE));
addTest('uuid.stringify()', uuid.stringify(uuid.parse(MY_NAMESPACE)));
addTest('uuid.validate()', uuid.validate(MY_NAMESPACE));
addTest('uuid.version()', uuid.version(MY_NAMESPACE));

done();
});
1 change: 1 addition & 0 deletions examples/browser-webpack/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
},
entry: {
all: './example-all.js',
allRequire: './example-all-require.js',
v1: './example-v1.js',
v4: './example-v4.js',

Expand Down
48 changes: 24 additions & 24 deletions examples/node-commonjs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 24 additions & 24 deletions examples/node-esmodules/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions examples/node-jest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# uuid example Node.js Jest

```
npm install
npm test
```
8 changes: 8 additions & 0 deletions examples/node-jest/jsdom.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @jest-environment jsdom */

const uuid = require('uuid');

test('uuidv4()', () => {
const val = uuid.v4();
expect(uuid.version(val)).toBe(4);
});
6 changes: 6 additions & 0 deletions examples/node-jest/node.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const uuid = require('uuid');

test('uuidv4()', () => {
const val = uuid.v4();
expect(uuid.version(val)).toBe(4);
});
Loading

0 comments on commit 8b6bf8f

Please sign in to comment.