From 0131f0c1b1c33fc2028a61c8dfc306464017bd85 Mon Sep 17 00:00:00 2001 From: Tom Byers Date: Tue, 21 May 2019 10:56:38 +0100 Subject: [PATCH] Move setup/teardown out of tests --- package.json | 2 +- tests/javascripts/autofocus.test.js | 11 +---------- tests/javascripts/collapsibleCheckboxes.test.js | 12 +----------- tests/javascripts/fullscreenTable.test.js | 11 +---------- tests/javascripts/jest.config.js | 3 +++ tests/javascripts/listEntry.test.js | 10 +--------- tests/javascripts/support/setup.js | 6 ++++++ tests/javascripts/support/teardown.js | 4 ++++ 8 files changed, 18 insertions(+), 41 deletions(-) create mode 100644 tests/javascripts/jest.config.js create mode 100644 tests/javascripts/support/setup.js create mode 100644 tests/javascripts/support/teardown.js diff --git a/package.json b/package.json index 71c11b281..098318c9f 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "node": "10.15.3" }, "scripts": { - "test": "gulp lint && jest tests/javascripts", + "test": "gulp lint && jest --config tests/javascripts/jest.config.js tests/javascripts", "build": "gulp", "watch": "gulp watch" }, diff --git a/tests/javascripts/autofocus.test.js b/tests/javascripts/autofocus.test.js index 8419b2d62..cae67996f 100644 --- a/tests/javascripts/autofocus.test.js +++ b/tests/javascripts/autofocus.test.js @@ -1,18 +1,9 @@ beforeAll(() => { - // set up jQuery - window.jQuery = require('jquery'); - $ = window.jQuery; - - // load module code - require('govuk_frontend_toolkit/javascripts/govuk/modules.js'); require('../../app/assets/javascripts/autofocus.js'); }); afterAll(() => { - window.jQuery = null; - $ = null; - - delete window.GOVUK; + require('./support/teardown.js'); }); describe('Autofocus', () => { diff --git a/tests/javascripts/collapsibleCheckboxes.test.js b/tests/javascripts/collapsibleCheckboxes.test.js index b29a4d624..88cc74e17 100644 --- a/tests/javascripts/collapsibleCheckboxes.test.js +++ b/tests/javascripts/collapsibleCheckboxes.test.js @@ -1,13 +1,6 @@ const helpers = require('./support/helpers'); beforeAll(() => { - // set up jQuery - window.jQuery = require('jquery'); - $ = window.jQuery; - - // load module code - require('govuk_frontend_toolkit/javascripts/govuk/modules.js'); - // TODO: remove this when tests for sticky JS are written require('../../app/assets/javascripts/stick-to-window-when-scrolling.js'); @@ -15,10 +8,7 @@ beforeAll(() => { }); afterAll(() => { - window.jQuery = null; - $ = null; - - delete window.GOVUK; + require('./support/teardown.js'); }); diff --git a/tests/javascripts/fullscreenTable.test.js b/tests/javascripts/fullscreenTable.test.js index 0db261211..0dba9f08b 100644 --- a/tests/javascripts/fullscreenTable.test.js +++ b/tests/javascripts/fullscreenTable.test.js @@ -1,20 +1,11 @@ const helpers = require('./support/helpers'); beforeAll(() => { - // set up jQuery - window.jQuery = require('jquery'); - $ = window.jQuery; - - // load module code - require('govuk_frontend_toolkit/javascripts/govuk/modules.js'); require('../../app/assets/javascripts/fullscreenTable.js'); }); afterAll(() => { - window.jQuery = null; - $ = null; - - delete window.GOVUK; + require('./support/teardown.js'); }); describe('FullscreenTable', () => { diff --git a/tests/javascripts/jest.config.js b/tests/javascripts/jest.config.js new file mode 100644 index 000000000..9fde149df --- /dev/null +++ b/tests/javascripts/jest.config.js @@ -0,0 +1,3 @@ +module.exports = { + setupFiles: ['./support/setup.js'] +} diff --git a/tests/javascripts/listEntry.test.js b/tests/javascripts/listEntry.test.js index 80c67e3ae..dd58bdeb3 100644 --- a/tests/javascripts/listEntry.test.js +++ b/tests/javascripts/listEntry.test.js @@ -1,19 +1,11 @@ beforeAll(() => { - // set up jQuery & HoganJS - window.jQuery = require('jquery'); - $ = window.jQuery; Hogan = require('hogan.js'); - // load module code - require('govuk_frontend_toolkit/javascripts/govuk/modules.js'); require('../../app/assets/javascripts/listEntry.js'); }); afterAll(() => { - window.jQuery = null; - $ = null; - - delete window.GOVUK; + require('./support/teardown.js'); }); describe("List entry", () => { diff --git a/tests/javascripts/support/setup.js b/tests/javascripts/support/setup.js new file mode 100644 index 000000000..be3fa1e6b --- /dev/null +++ b/tests/javascripts/support/setup.js @@ -0,0 +1,6 @@ +// set up jQuery +window.jQuery = require('jquery'); +$ = window.jQuery; + +// load module code +require('govuk_frontend_toolkit/javascripts/govuk/modules.js'); diff --git a/tests/javascripts/support/teardown.js b/tests/javascripts/support/teardown.js new file mode 100644 index 000000000..c7d00bf8b --- /dev/null +++ b/tests/javascripts/support/teardown.js @@ -0,0 +1,4 @@ +window.jQuery = null; +$ = null; + +delete window.GOVUK;