Skip to content

Commit

Permalink
Migrate legacy reference test to node test runner (duckduckgo#2118)
Browse files Browse the repository at this point in the history
* PoC: running tests in node

* Move cookie reference tests to the node runner

* Port fingerprinting-protection-test

* Port https-upgrades-tests

* Port privacy-configuration-tests

* Add node-tests to npm test

* Tidy dead code

* Add a readme.

* Bump reference tests

* Bump content-scope-scripts

* Move https-upgrades-tests to the browser test runner

* Bump content-scope-scripts
  • Loading branch information
sammacbeth authored Jul 24, 2023
1 parent 4426bea commit d2646f5
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 43 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,5 @@ The extension imports several DDG-owned modules (see [package.json](https://gith
- TypeScript Linting: `npm run tsc`

### Testing
- Unit tests: `npm test`
- Unit tests: `npm test` (full docs [here](./unit-test/README.md))
- Integration tests: `npm run playwright` (full docs [here](./integration-test/README.md))
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ unit-test: build/test/legacy-background.js

.PHONY: unit-test

NODE_TESTS = unit-test/node/**/*.js
node-test:
$(ESBUILD) --platform=node --outdir=build/node --inject:./unit-test/inject-chrome-shim.js --external:jsdom $(NODE_TESTS)
node_modules/.bin/jasmine build/node/*.js

## npm: Pull in the external dependencies (npm install).
npm:
npm ci --ignore-scripts
Expand Down Expand Up @@ -230,7 +235,7 @@ JS_BUNDLES = background.js base.js feedback.js options.js devtools-panel.js list
BUILD_TARGETS = $(addprefix $(BUILD_DIR)/public/js/, $(JS_BUNDLES))

## Unit tests scripts.
UNIT_TEST_SRC = unit-test/legacy/*.js unit-test/legacy/reference-tests/*.js
UNIT_TEST_SRC = unit-test/legacy/*.js
build/test:
mkdir -p $@

Expand Down
203 changes: 197 additions & 6 deletions package-lock.json

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

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"eslint": "eslint '*.js' shared/js shared/data unit-test integration-test scripts",
"eslint-fix": "npm run eslint -- --fix",
"lint": "npm run eslint && npm run tsc",
"test": "make unit-test",
"test": "npm run test.unit && npm run test.node",
"test.unit": "make unit-test",
"test.node": "make node-test",
"playwright": "make dev browser=chrome type=dev && playwright test",
"playwright-mv3": "make dev browser=chrome-mv3 type=dev && PW_EXPERIMENTAL_SERVICE_WORKER_NETWORK_EVENTS=1 playwright test",
"tsc": "tsc",
Expand Down Expand Up @@ -49,6 +51,7 @@
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-node": "^11.1.0",
"fake-indexeddb": "^4.0.2",
"glob": "^10.3.3",
"jasmine": "5.0.2",
"jsdom": "^21.1.1",
Expand All @@ -67,7 +70,7 @@
},
"dependencies": {
"@duckduckgo/autofill": "github:duckduckgo/duckduckgo-autofill#8.0.0",
"@duckduckgo/content-scope-scripts": "github:duckduckgo/content-scope-scripts#4.24.0",
"@duckduckgo/content-scope-scripts": "github:duckduckgo/content-scope-scripts#4.27.1",
"@duckduckgo/ddg2dnr": "file:packages/ddg2dnr",
"@duckduckgo/jsbloom": "^1.0.2",
"@duckduckgo/privacy-dashboard": "github:duckduckgo/privacy-dashboard#1.6.1",
Expand Down
2 changes: 1 addition & 1 deletion shared/js/background/storage/tds.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class TDSStorage {

_internalOnListUpdate (configName, configValue) {
return new Promise((resolve, reject) => {
self.setTimeout(async () => {
setTimeout(async () => {
// Ensure the onReady promise for this configuration is resolved.
try {
const readyResolve = this._onReadyResolvers.get(configName)
Expand Down
14 changes: 14 additions & 0 deletions unit-test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Unit tests

This folder contains unit-tests, which test individual components and functions in the source code. These tests
are written using the [Jasmine](https://jasmine.github.io/) testing framework.
Tests are bundled with `esbuild` (with the exception of tests in the `legacy` folder, which still use `browserify`), then run in one of two test environments:
1. Node: these tests run in a plain node process. Tests placed in the `node` folder will be run in this environment.
2. Browser: the rest of the tests are run in a headless chrome browser (using `karma`'s test runner).

## Running tests

Tests can be run as follows:
- `npm test`: Run all tests (both node and browser).
- `npm test.unit`: Run only browser-based unit tests.
- `npm test.node`: Run only node-based unit tests.
Loading

0 comments on commit d2646f5

Please sign in to comment.