test: migrate fixtures.spec.ts to use beforeEach/afterEach (#6029)

This commit is contained in:
Dmitry Gozman 2021-03-31 19:38:06 -07:00 committed by GitHub
parent 16d98cb48a
commit b81238ca61
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,123 +15,135 @@
* limitations under the License. * limitations under the License.
*/ */
import { folio, RemoteServer } from './remoteServer.fixture'; import { folio } from './remoteServer.fixture';
import { execSync } from 'child_process'; import { execSync } from 'child_process';
import path from 'path'; import path from 'path';
import * as stackTrace from '../src/utils/stackTrace'; import * as stackTrace from '../src/utils/stackTrace';
import { setUnderTest } from '../src/utils/utils'; import { setUnderTest } from '../src/utils/utils';
import type { Browser } from '../index';
type FixturesFixtures = { const { it, describe, expect, beforeEach, afterEach } = folio;
connectedRemoteServer: RemoteServer;
stallingConnectedRemoteServer: RemoteServer;
};
const fixtures = folio.extend<FixturesFixtures>();
fixtures.connectedRemoteServer.init(async ({browserType, remoteServer, server}, run) => {
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
const page = await browser.newPage();
await page.goto(server.EMPTY_PAGE);
await run(remoteServer);
await browser.close();
});
fixtures.stallingConnectedRemoteServer.init(async ({browserType, stallingRemoteServer, server}, run) => {
const browser = await browserType.connect({ wsEndpoint: stallingRemoteServer.wsEndpoint() });
const page = await browser.newPage();
await page.goto(server.EMPTY_PAGE);
await run(stallingRemoteServer);
await browser.close();
});
const { it, describe, expect } = fixtures.build();
it('should close the browser when the node process closes', test => { it('should close the browser when the node process closes', test => {
test.slow(); test.slow();
}, async ({connectedRemoteServer, isWindows}) => { }, async ({browserType, remoteServer, isWindows, server}) => {
const browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
const page = await browser.newPage();
await page.goto(server.EMPTY_PAGE);
if (isWindows) if (isWindows)
execSync(`taskkill /pid ${connectedRemoteServer.child().pid} /T /F`); execSync(`taskkill /pid ${remoteServer.child().pid} /T /F`);
else else
process.kill(connectedRemoteServer.child().pid); process.kill(remoteServer.child().pid);
expect(await connectedRemoteServer.childExitCode()).toBe(isWindows ? 1 : 0); const exitCode = await remoteServer.childExitCode();
await browser.close();
// We might not get browser exitCode in time when killing the parent node process, // We might not get browser exitCode in time when killing the parent node process,
// so we don't check it here. // so we don't check it here.
expect(exitCode).toBe(isWindows ? 1 : 0);
}); });
describe('fixtures', (suite, { platform, headful }) => { describe('signals', (suite, { platform, headful }) => {
suite.skip(platform === 'win32' || headful); suite.skip(platform === 'win32' || headful);
suite.slow(); suite.slow();
}, () => { }, () => {
// Cannot reliably send signals on Windows. let browser: Browser;
it('should report browser close signal', async ({connectedRemoteServer}) => {
const pid = await connectedRemoteServer.out('pid'); beforeEach(async ({ browserType, server, remoteServer }) => {
process.kill(-pid, 'SIGTERM'); browser = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
expect(await connectedRemoteServer.out('exitCode')).toBe('null'); const page = await browser.newPage();
expect(await connectedRemoteServer.out('signal')).toBe('SIGTERM'); await page.goto(server.EMPTY_PAGE);
process.kill(connectedRemoteServer.child().pid);
await connectedRemoteServer.childExitCode();
}); });
it('should report browser close signal 2', async ({connectedRemoteServer}) => { afterEach(async () => {
const pid = await connectedRemoteServer.out('pid'); await browser.close();
});
it('should report browser close signal', async ({remoteServer}) => {
const pid = await remoteServer.out('pid');
process.kill(-pid, 'SIGTERM');
expect(await remoteServer.out('exitCode')).toBe('null');
expect(await remoteServer.out('signal')).toBe('SIGTERM');
process.kill(remoteServer.child().pid);
await remoteServer.childExitCode();
});
it('should report browser close signal 2', async ({remoteServer}) => {
const pid = await remoteServer.out('pid');
process.kill(-pid, 'SIGKILL'); process.kill(-pid, 'SIGKILL');
expect(await connectedRemoteServer.out('exitCode')).toBe('null'); expect(await remoteServer.out('exitCode')).toBe('null');
expect(await connectedRemoteServer.out('signal')).toBe('SIGKILL'); expect(await remoteServer.out('signal')).toBe('SIGKILL');
process.kill(connectedRemoteServer.child().pid); process.kill(remoteServer.child().pid);
await connectedRemoteServer.childExitCode(); await remoteServer.childExitCode();
}); });
it('should close the browser on SIGINT', (test, { browserChannel }) => { it('should close the browser on SIGINT', (test, { browserChannel }) => {
test.fixme(!!browserChannel, 'Uncomment on roll'); test.fixme(!!browserChannel, 'Uncomment on roll');
}, async ({connectedRemoteServer}) => { }, async ({remoteServer}) => {
process.kill(connectedRemoteServer.child().pid, 'SIGINT'); process.kill(remoteServer.child().pid, 'SIGINT');
expect(await connectedRemoteServer.out('exitCode')).toBe('0'); expect(await remoteServer.out('exitCode')).toBe('0');
expect(await connectedRemoteServer.out('signal')).toBe('null'); expect(await remoteServer.out('signal')).toBe('null');
expect(await connectedRemoteServer.childExitCode()).toBe(130); expect(await remoteServer.childExitCode()).toBe(130);
}); });
it('should close the browser on SIGTERM', (test, { browserChannel }) => { it('should close the browser on SIGTERM', (test, { browserChannel }) => {
test.fixme(!!browserChannel, 'Uncomment on roll'); test.fixme(!!browserChannel, 'Uncomment on roll');
}, async ({connectedRemoteServer}) => { }, async ({remoteServer}) => {
process.kill(connectedRemoteServer.child().pid, 'SIGTERM'); process.kill(remoteServer.child().pid, 'SIGTERM');
expect(await connectedRemoteServer.out('exitCode')).toBe('0'); expect(await remoteServer.out('exitCode')).toBe('0');
expect(await connectedRemoteServer.out('signal')).toBe('null'); expect(await remoteServer.out('signal')).toBe('null');
expect(await connectedRemoteServer.childExitCode()).toBe(0); expect(await remoteServer.childExitCode()).toBe(0);
}); });
it('should close the browser on SIGHUP', (test, { browserChannel }) => { it('should close the browser on SIGHUP', (test, { browserChannel }) => {
test.fixme(!!browserChannel, 'Uncomment on roll'); test.fixme(!!browserChannel, 'Uncomment on roll');
}, async ({connectedRemoteServer}) => { }, async ({remoteServer}) => {
process.kill(connectedRemoteServer.child().pid, 'SIGHUP'); process.kill(remoteServer.child().pid, 'SIGHUP');
expect(await connectedRemoteServer.out('exitCode')).toBe('0'); expect(await remoteServer.out('exitCode')).toBe('0');
expect(await connectedRemoteServer.out('signal')).toBe('null'); expect(await remoteServer.out('signal')).toBe('null');
expect(await connectedRemoteServer.childExitCode()).toBe(0); expect(await remoteServer.childExitCode()).toBe(0);
});
});
describe('stalling signals', (suite, { platform, headful }) => {
suite.skip(platform === 'win32' || headful);
suite.slow();
}, () => {
let browser: Browser;
beforeEach(async ({ browserType, server, stallingRemoteServer }) => {
browser = await browserType.connect({ wsEndpoint: stallingRemoteServer.wsEndpoint() });
const page = await browser.newPage();
await page.goto(server.EMPTY_PAGE);
}); });
it('should kill the browser on double SIGINT', async ({stallingConnectedRemoteServer}) => { afterEach(async () => {
process.kill(stallingConnectedRemoteServer.child().pid, 'SIGINT'); await browser.close();
await stallingConnectedRemoteServer.out('stalled');
process.kill(stallingConnectedRemoteServer.child().pid, 'SIGINT');
expect(await stallingConnectedRemoteServer.out('exitCode')).toBe('null');
expect(await stallingConnectedRemoteServer.out('signal')).toBe('SIGKILL');
expect(await stallingConnectedRemoteServer.childExitCode()).toBe(130);
}); });
it('should kill the browser on SIGINT + SIGTERM', async ({stallingConnectedRemoteServer}) => { it('should kill the browser on double SIGINT', async ({stallingRemoteServer}) => {
process.kill(stallingConnectedRemoteServer.child().pid, 'SIGINT'); process.kill(stallingRemoteServer.child().pid, 'SIGINT');
await stallingConnectedRemoteServer.out('stalled'); await stallingRemoteServer.out('stalled');
process.kill(stallingConnectedRemoteServer.child().pid, 'SIGTERM'); process.kill(stallingRemoteServer.child().pid, 'SIGINT');
expect(await stallingConnectedRemoteServer.out('exitCode')).toBe('null'); expect(await stallingRemoteServer.out('exitCode')).toBe('null');
expect(await stallingConnectedRemoteServer.out('signal')).toBe('SIGKILL'); expect(await stallingRemoteServer.out('signal')).toBe('SIGKILL');
expect(await stallingConnectedRemoteServer.childExitCode()).toBe(0); expect(await stallingRemoteServer.childExitCode()).toBe(130);
}); });
it('should kill the browser on SIGTERM + SIGINT', async ({stallingConnectedRemoteServer}) => { it('should kill the browser on SIGINT + SIGTERM', async ({stallingRemoteServer}) => {
process.kill(stallingConnectedRemoteServer.child().pid, 'SIGTERM'); process.kill(stallingRemoteServer.child().pid, 'SIGINT');
await stallingConnectedRemoteServer.out('stalled'); await stallingRemoteServer.out('stalled');
process.kill(stallingConnectedRemoteServer.child().pid, 'SIGINT'); process.kill(stallingRemoteServer.child().pid, 'SIGTERM');
expect(await stallingConnectedRemoteServer.out('exitCode')).toBe('null'); expect(await stallingRemoteServer.out('exitCode')).toBe('null');
expect(await stallingConnectedRemoteServer.out('signal')).toBe('SIGKILL'); expect(await stallingRemoteServer.out('signal')).toBe('SIGKILL');
expect(await stallingConnectedRemoteServer.childExitCode()).toBe(130); expect(await stallingRemoteServer.childExitCode()).toBe(0);
});
it('should kill the browser on SIGTERM + SIGINT', async ({stallingRemoteServer}) => {
process.kill(stallingRemoteServer.child().pid, 'SIGTERM');
await stallingRemoteServer.out('stalled');
process.kill(stallingRemoteServer.child().pid, 'SIGINT');
expect(await stallingRemoteServer.out('exitCode')).toBe('null');
expect(await stallingRemoteServer.out('signal')).toBe('SIGKILL');
expect(await stallingRemoteServer.childExitCode()).toBe(130);
}); });
}); });