From 534dedec3b6ab12ac4b3a9a8b372d0ecedfa1fa9 Mon Sep 17 00:00:00 2001 From: Yuta Hiroto Date: Sat, 15 Jun 2019 14:36:28 +0100 Subject: [PATCH] test: update syntax --- package-lock.json | 29 ++-- test/e2e/Client.test.js | 184 ++++++++++++-------------- test/e2e/ClientOptions.test.js | 213 +++++++++++++----------------- test/e2e/ProvidePlugin.test.js | 57 ++++---- test/server/utils/runOpen.test.js | 36 ++--- 5 files changed, 243 insertions(+), 276 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1349179b8b..2dfcbcf368 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9896,6 +9896,15 @@ "resolved": "https://registry.npmjs.org/mime/-/mime-2.4.4.tgz", "integrity": "sha512-LRxmNwziLPT828z+4YkNzloCFC2YM4wrB99k+AV5ZbEyfGNWfG8SO1FUXLmLDBSo89NrJZ4DIWeLjy1CHGhMGA==", "dev": true + }, + "ws": { + "version": "6.2.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-6.2.1.tgz", + "integrity": "sha512-GIyAXC2cB7LjvpgMt9EKS2ldqr0MTrORaleiOno6TweZ6r3TKtoFQWay/2PceJ3RuBasOHzXNn5Lrw1X0bEjqA==", + "dev": true, + "requires": { + "async-limiter": "~1.0.0" + } } } }, @@ -12037,9 +12046,9 @@ } }, "url-loader": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/url-loader/-/url-loader-1.1.2.tgz", - "integrity": "sha512-dXHkKmw8FhPqu8asTc1puBfe3TehOCo2+RmOOev5suNCIYBcT626kxiWg1NBVkwc4rO8BGa7gP70W7VXuqHrjg==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/url-loader/-/url-loader-2.0.0.tgz", + "integrity": "sha512-RT3cz+Lw+kFMCKLabMyogFrc/aRoAMJHmY5os1VDBlav+4t5vxUxKDn47zCSFAYAl+hM8YLg4TZwG4TkJaafkg==", "dev": true, "requires": { "loader-utils": "^1.1.0", @@ -12048,9 +12057,9 @@ }, "dependencies": { "mime": { - "version": "2.4.3", - "resolved": "https://registry.npmjs.org/mime/-/mime-2.4.3.tgz", - "integrity": "sha512-QgrPRJfE+riq5TPZMcHZOtm8c6K/yYrMbKIoRfapfiGLxS8OTeIfRhUGW5LU7MlRa52KOAGCfUNruqLrIBvWZw==", + "version": "2.4.4", + "resolved": "https://registry.npmjs.org/mime/-/mime-2.4.4.tgz", + "integrity": "sha512-LRxmNwziLPT828z+4YkNzloCFC2YM4wrB99k+AV5ZbEyfGNWfG8SO1FUXLmLDBSo89NrJZ4DIWeLjy1CHGhMGA==", "dev": true } } @@ -12548,12 +12557,12 @@ } }, "ws": { - "version": "6.2.1", - "resolved": "https://registry.npmjs.org/ws/-/ws-6.2.1.tgz", - "integrity": "sha512-GIyAXC2cB7LjvpgMt9EKS2ldqr0MTrORaleiOno6TweZ6r3TKtoFQWay/2PceJ3RuBasOHzXNn5Lrw1X0bEjqA==", + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.0.1.tgz", + "integrity": "sha512-ILHfMbuqLJvnSgYXLgy4kMntroJpe8hT41dOVWM8bxRuw6TK4mgMp9VJUNsZTEc5Bh+Mbs0DJT4M0N+wBG9l9A==", "dev": true, "requires": { - "async-limiter": "~1.0.0" + "async-limiter": "^1.0.0" } }, "xml": { diff --git a/test/e2e/Client.test.js b/test/e2e/Client.test.js index 90eb2ffe5a..3995fec5a6 100644 --- a/test/e2e/Client.test.js +++ b/test/e2e/Client.test.js @@ -37,56 +37,50 @@ describe('reload', () => { }); describe('on browser client', () => { - it('should hot reload without page refresh', (done) => { - runBrowser().then(({ page, browser }) => { - let refreshed = false; - page.waitForNavigation({ waitUntil: 'load' }).then(() => { - page - .evaluate(() => { - const body = document.body; - const bgColor = getComputedStyle(body)['background-color']; - return bgColor; - }) - .then((color) => { - page.setRequestInterception(true).then(() => { - page.on('request', (req) => { - if ( - req.isNavigationRequest() && - req.frame() === page.mainFrame() && - req.url() === `http://localhost:${port}/main` - ) { - refreshed = true; - } - req.continue(); - }); - fs.writeFileSync( - cssFilePath, - 'body { background-color: rgb(255, 0, 0); }' - ); - page.waitFor(10000).then(() => { - page - .evaluate(() => { - const body = document.body; - const bgColor = getComputedStyle(body)[ - 'background-color' - ]; - return bgColor; - }) - .then((color2) => { - browser.close().then(() => { - expect(color).toEqual('rgb(0, 0, 255)'); - expect(color2).toEqual('rgb(255, 0, 0)'); - expect(refreshed).toBeFalsy(); - done(); - }); - }); - }); - }); - }); - }); - - page.goto(`http://localhost:${port}/main`); + it('should hot reload without page refresh', async () => { + let refreshed = false; + const { page, browser } = await runBrowser(); + + page.goto(`http://localhost:${port}/main`); + + await page.waitForNavigation({ waitUntil: 'load' }); + + const color = await page.evaluate(() => { + const body = document.body; + const bgColor = getComputedStyle(body)['background-color']; + return bgColor; + }); + + await page.setRequestInterception(true); + + page.on('request', (req) => { + if ( + req.isNavigationRequest() && + req.frame() === page.mainFrame() && + req.url() === `http://localhost:${port}/main` + ) { + refreshed = true; + } + req.continue(); }); + fs.writeFileSync( + cssFilePath, + 'body { background-color: rgb(255, 0, 0); }' + ); + + await page.waitFor(10000); + + const color2 = await page.evaluate(() => { + const body = document.body; + const bgColor = getComputedStyle(body)['background-color']; + return bgColor; + }); + + await browser.close(); + + expect(color).toEqual('rgb(0, 0, 255)'); + expect(color2).toEqual('rgb(255, 0, 0)'); + expect(refreshed).toBeFalsy(); }); }); }); @@ -115,56 +109,50 @@ describe('reload', () => { }); describe('on browser client', () => { - it('should reload with page refresh', (done) => { - runBrowser().then(({ page, browser }) => { - let refreshed = false; - page.waitForNavigation({ waitUntil: 'load' }).then(() => { - page - .evaluate(() => { - const body = document.body; - const bgColor = getComputedStyle(body)['background-color']; - return bgColor; - }) - .then((color) => { - page.setRequestInterception(true).then(() => { - page.on('request', (req) => { - if ( - req.isNavigationRequest() && - req.frame() === page.mainFrame() && - req.url() === `http://localhost:${port}/main` - ) { - refreshed = true; - } - req.continue(); - }); - fs.writeFileSync( - cssFilePath, - 'body { background-color: rgb(255, 0, 0); }' - ); - page.waitFor(10000).then(() => { - page - .evaluate(() => { - const body = document.body; - const bgColor = getComputedStyle(body)[ - 'background-color' - ]; - return bgColor; - }) - .then((color2) => { - browser.close().then(() => { - expect(color).toEqual('rgb(0, 0, 255)'); - expect(color2).toEqual('rgb(255, 0, 0)'); - expect(refreshed).toBeTruthy(); - done(); - }); - }); - }); - }); - }); - }); - - page.goto(`http://localhost:${port}/main`); + it('should reload with page refresh', async () => { + let refreshed = false; + const { page, browser } = await runBrowser(); + + page.goto(`http://localhost:${port}/main`); + + await page.waitForNavigation({ waitUntil: 'load' }); + + const color = await page.evaluate(() => { + const body = document.body; + const bgColor = getComputedStyle(body)['background-color']; + return bgColor; + }); + + await page.setRequestInterception(true); + + page.on('request', (req) => { + if ( + req.isNavigationRequest() && + req.frame() === page.mainFrame() && + req.url() === `http://localhost:${port}/main` + ) { + refreshed = true; + } + req.continue(); }); + fs.writeFileSync( + cssFilePath, + 'body { background-color: rgb(255, 0, 0); }' + ); + + await page.waitFor(10000); + + const color2 = await page.evaluate(() => { + const body = document.body; + const bgColor = getComputedStyle(body)['background-color']; + return bgColor; + }); + + await browser.close(); + + expect(color).toEqual('rgb(0, 0, 255)'); + expect(color2).toEqual('rgb(255, 0, 0)'); + expect(refreshed).toBeTruthy(); }); }); }); diff --git a/test/e2e/ClientOptions.test.js b/test/e2e/ClientOptions.test.js index 07d3bba8d9..9c7f119a48 100644 --- a/test/e2e/ClientOptions.test.js +++ b/test/e2e/ClientOptions.test.js @@ -7,6 +7,7 @@ const testServer = require('../helpers/test-server'); const config = require('../fixtures/client-config/webpack.config'); const runBrowser = require('../helpers/run-browser'); const [port1, port2, port3] = require('../ports-map').ClientOptions; +const timer = require('../helpers/timer'); describe('Client code', () => { function startProxy(port) { @@ -48,38 +49,33 @@ describe('Client code', () => { }); afterAll((done) => { - proxy.close(() => { - done(); - }); + proxy.close(done); }); - it('responds with a 200', (done) => { + it('responds with a 200', async () => { { const req = request(`http://localhost:${port2}`); - req.get('/sockjs-node').expect(200, 'Welcome to SockJS!\n', done); + await req.get('/sockjs-node').expect(200, 'Welcome to SockJS!\n'); } { const req = request(`http://localhost:${port1}`); - req.get('/sockjs-node').expect(200, 'Welcome to SockJS!\n', done); + await req.get('/sockjs-node').expect(200, 'Welcome to SockJS!\n'); } }); - it('requests websocket through the proxy with proper port number', (done) => { - runBrowser().then(({ page, browser }) => { - page - .waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/)) - .then((requestObj) => { - browser.close().then(() => { - expect( - requestObj - .url() - .includes(`http://localhost:${port1}/sockjs-node`) - ).toBeTruthy(); - done(); - }); - }); - page.goto(`http://localhost:${port2}/main`); - }); + it('requests websocket through the proxy with proper port number', async () => { + const { page, browser } = await runBrowser(); + + page.goto(`http://localhost:${port2}/main`); + const req = await page.waitForRequest((requestObj) => + requestObj.url().match(/sockjs-node/) + ); + + await browser.close(); + + expect( + req.url().includes(`http://localhost:${port1}/sockjs-node`) + ).toBeTruthy(); }); }); }); @@ -102,24 +98,20 @@ describe('Client complex inline script path', () => { afterAll(testServer.close); describe('browser client', () => { - it('uses the correct public hostname and sockPath', (done) => { - runBrowser().then(({ page, browser }) => { - page - .waitForRequest((requestObj) => - requestObj.url().match(/foo\/test\/bar/) - ) - .then((requestObj) => { - browser.close().then(() => { - expect( - requestObj - .url() - .includes(`http://myhost.test:${port2}/foo/test/bar/`) - ).toBeTruthy(); - done(); - }); - }); - page.goto(`http://localhost:${port2}/main`); - }); + it('uses the correct public hostname and sockPath', async () => { + const { page, browser } = await runBrowser(); + + page.goto(`http://localhost:${port2}/main`); + + const req = await page.waitForRequest((requestObj) => + requestObj.url().match(/foo\/test\/bar/) + ); + + await browser.close(); + + expect( + req.url().includes(`http://myhost.test:${port2}/foo/test/bar/`) + ).toBeTruthy(); }); }); }); @@ -142,25 +134,20 @@ describe('Client complex inline script path with sockPort', () => { afterAll(testServer.close); describe('browser client', () => { - it('uses the correct sockPort', (done) => { - runBrowser().then(({ page, browser }) => { - page - .waitForRequest((requestObj) => - requestObj.url().match(/foo\/test\/bar/) - ) - .then((requestObj) => { - browser.close().then(() => { - expect( - requestObj - .url() - .includes(`http://localhost:${port3}/foo/test/bar`) - ).toBeTruthy(); - done(); - }); - }); - - page.goto(`http://localhost:${port2}/main`); - }); + it('uses the correct sockPort', async () => { + const { page, browser } = await runBrowser(); + + page.goto(`http://localhost:${port2}/main`); + + const req = await page.waitForRequest((requestObj) => + requestObj.url().match(/foo\/test\/bar/) + ); + + await browser.close(); + + expect( + req.url().includes(`http://localhost:${port3}/foo/test/bar`) + ).toBeTruthy(); }); }); }); @@ -185,22 +172,19 @@ describe('Client complex inline script path with sockPort, no sockPath', () => { afterAll(testServer.close); describe('browser client', () => { - it('uses the correct sockPort and sockPath', (done) => { - runBrowser().then(({ page, browser }) => { - page - .waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/)) - .then((requestObj) => { - browser.close().then(() => { - expect( - requestObj - .url() - .includes(`http://localhost:${port3}/sockjs-node`) - ).toBeTruthy(); - done(); - }); - }); - page.goto(`http://localhost:${port2}/main`); - }); + it('uses the correct sockPort and sockPath', async () => { + const { page, browser } = await runBrowser(); + + page.goto(`http://localhost:${port2}/main`); + const req = await page.waitForRequest((requestObj) => + requestObj.url().match(/sockjs-node/) + ); + + await browser.close(); + + expect( + req.url().includes(`http://localhost:${port3}/sockjs-node`) + ).toBeTruthy(); }); }); }); @@ -222,22 +206,19 @@ describe('Client complex inline script path with sockHost', () => { afterAll(testServer.close); describe('browser client', () => { - it('uses the correct sockHost', (done) => { - runBrowser().then(({ page, browser }) => { - page - .waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/)) - .then((requestObj) => { - browser.close().then(() => { - expect( - requestObj - .url() - .includes(`http://myhost.test:${port2}/sockjs-node`) - ).toBeTruthy(); - done(); - }); - }); - page.goto(`http://localhost:${port2}/main`); - }); + it('uses the correct sockHost', async () => { + const { page, browser } = await runBrowser(); + + page.goto(`http://localhost:${port2}/main`); + + const req = await page.waitForRequest((requestObj) => + requestObj.url().match(/sockjs-node/) + ); + await browser.close(); + + expect( + req.url().includes(`http://myhost.test:${port2}/sockjs-node`) + ).toBeTruthy(); }); }); }); @@ -281,37 +262,31 @@ describe('Client console.log', () => { ]; for (const { title, options } of cases) { - it(title, () => { + it(title, async () => { const res = []; const testOptions = Object.assign({}, baseOptions, options); - // TODO: use async/await when Node.js v6 support is dropped - return Promise.resolve() - .then(() => { - return new Promise((resolve) => { - testServer.startAwaitingCompilation(config, testOptions, resolve); - }); - }) - .then(runBrowser) - .then(({ page, browser }) => { - return new Promise((resolve) => { - page.goto(`http://localhost:${port2}/main`); - page.on('console', ({ _text }) => { - res.push(_text); - }); - setTimeout(() => { - browser.close().then(() => { - expect(res).toMatchSnapshot(); - resolve(); - }); - }, 1000); - }); - }) - .then(() => { - return new Promise((resolve) => { - testServer.close(resolve); - }); - }); + // TODO: refactor(hiroppy) + await new Promise((resolve) => { + testServer.startAwaitingCompilation(config, testOptions, resolve); + }); + + const { page, browser } = await runBrowser(); + + page.goto(`http://localhost:${port2}/main`); + page.on('console', ({ _text }) => { + res.push(_text); + }); + + await timer(3000); + + expect(res).toMatchSnapshot(); + await browser.close(); + + // TODO: refactor(hiroppy) + await new Promise((resolve) => { + testServer.close(resolve); + }); }); } }); diff --git a/test/e2e/ProvidePlugin.test.js b/test/e2e/ProvidePlugin.test.js index 99d44cf910..38eaa089dd 100644 --- a/test/e2e/ProvidePlugin.test.js +++ b/test/e2e/ProvidePlugin.test.js @@ -22,22 +22,19 @@ describe('ProvidePlugin', () => { afterAll(testServer.close); describe('on browser client', () => { - it('should inject SockJS client implementation', (done) => { - runBrowser().then(({ page, browser }) => { - page.waitForNavigation({ waitUntil: 'load' }).then(() => { - page - .evaluate(() => { - return window.injectedClient === window.expectedClient; - }) - .then((isCorrectClient) => { - browser.close().then(() => { - expect(isCorrectClient).toBeTruthy(); - done(); - }); - }); - }); - page.goto(`http://localhost:${port}/main`); + it('should inject SockJS client implementation', async () => { + const { page, browser } = await runBrowser(); + + page.goto(`http://localhost:${port}/main`); + await page.waitForNavigation({ waitUntil: 'load' }); + + const isCorrectClient = await page.evaluate(() => { + return window.injectedClient === window.expectedClient; }); + + await browser.close(); + + expect(isCorrectClient).toBeTruthy(); }); }); }); @@ -58,23 +55,21 @@ describe('ProvidePlugin', () => { afterAll(testServer.close); describe('on browser client', () => { - it('should not inject client implementation', (done) => { - runBrowser().then(({ page, browser }) => { - page.waitForNavigation({ waitUntil: 'load' }).then(() => { - page - .evaluate(() => { - // eslint-disable-next-line no-undefined - return window.injectedClient === undefined; - }) - .then((isCorrectClient) => { - browser.close().then(() => { - expect(isCorrectClient).toBeTruthy(); - done(); - }); - }); - }); - page.goto(`http://localhost:${port}/main`); + it('should not inject client implementation', async () => { + const { page, browser } = await runBrowser(); + + page.goto(`http://localhost:${port}/main`); + + await page.waitForNavigation({ waitUntil: 'load' }); + + const isCorrectClient = await page.evaluate(() => { + // eslint-disable-next-line no-undefined + return window.injectedClient === undefined; }); + + await browser.close(); + + expect(isCorrectClient).toBeTruthy(); }); }); }); diff --git a/test/server/utils/runOpen.test.js b/test/server/utils/runOpen.test.js index 296450daba..1ae6b092d1 100644 --- a/test/server/utils/runOpen.test.js +++ b/test/server/utils/runOpen.test.js @@ -75,16 +75,17 @@ describe('runOpen util', () => { }, ] `); - }); }); + }); - it('on specify absolute https URL with page in Google Chrome ', () => { - return runOpen( - 'https://example.com', - { open: 'Google Chrome', openPage: 'https://example2.com' }, - console - ).then(() => { - expect(opn.mock.calls[0]).toMatchInlineSnapshot(` + it('on specify absolute https URL with page in Google Chrome ', async () => { + await runOpen( + 'https://example.com', + { open: 'Google Chrome', openPage: 'https://example2.com' }, + console + ); + + expect(open.mock.calls[0]).toMatchInlineSnapshot(` Array [ "https://example2.com", Object { @@ -93,16 +94,16 @@ describe('runOpen util', () => { }, ] `); - }); - }); + }); - it('on specify absolute http URL with page in Google Chrome ', () => { - return runOpen( - 'https://example.com', - { open: 'Google Chrome', openPage: 'http://example2.com' }, - console - ).then(() => { - expect(opn.mock.calls[0]).toMatchInlineSnapshot(` + it('on specify absolute http URL with page in Google Chrome ', async () => { + runOpen( + 'https://example.com', + { open: 'Google Chrome', openPage: 'http://example2.com' }, + console + ); + + expect(open.mock.calls[0]).toMatchInlineSnapshot(` Array [ "http://example2.com", Object { @@ -111,7 +112,6 @@ describe('runOpen util', () => { }, ] `); - }); }); describe('should not open browser', () => {