-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix tests to cover uncovered branches
This commit solves the coverage drop from: - #23 01a79f6 Restores `istanbul` as the coverage reporter for the JSDom-based CI run to restore consistency in reporting, particularly in Coveralls. --- Adapted from the new test/missing-app-div/jsdom.test.js: Moved the "missing #app div" test into a separate directory from the other tests in ../jsdom.test.js. to prevent Node.js from using the same `test-modules/main.js` import. Otherwise: - If the test case were in the same file, the "missing #app div" branch wouldn't execute and the test would fail. - If this test file were in the same directory, the Istanbul coverage reporter wouldn't see the coverage from the "missing #app div" branch. I don't know exactly why that is. At the same time, the previous `src="./main.js?version=missing"` query suffix is no longer necessary. I got the idea to organize the tests this way after successfully covering similar code in: - mbland/tomcat-servlet-testing-example#85 mbland/tomcat-servlet-testing-example@b5df30e
- Loading branch information
Showing
6 changed files
with
79 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
export default class JsdomFixture { | ||
static #origWindow = globalThis.window | ||
static #origDocument = globalThis.document | ||
|
||
restoreOrigWindowAndDocument() { | ||
globalThis.window = JsdomFixture.#origWindow | ||
globalThis.document = JsdomFixture.#origDocument | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* eslint-env browser */ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import JsdomPageOpener from '../../lib/jsdom.js' | ||
import JsdomFixture from '../jsdom-fixture.js' | ||
import { afterEach, beforeAll, describe, expect, test, vi } from 'vitest' | ||
|
||
// This is in a separate directory from the other tests in ../jsdom.test.js. to | ||
// prevent Node.js from using the same `test-modules/main.js` import. Otherwise: | ||
// | ||
// - If the test case were in the same file, the "missing #app div" branch | ||
// wouldn't execute and the test would fail. | ||
// | ||
// - If this test file were in the same directory, the Istanbul coverage | ||
// reporter wouldn't see the coverage from the "missing #app div" branch. I | ||
// don't know exactly why that is. | ||
// | ||
// At the same time, the previous `src="./main.js?version=missing"` query suffix | ||
// is no longer necessary. | ||
// | ||
// This solves the coverage drop from: | ||
// | ||
// - mbland/test-page-opener#23 | ||
// mbland/test-page-opener@01a79f6 | ||
// | ||
// I got the idea to organize the tests this way after successfully covering | ||
// similar code in: | ||
// | ||
// - mbland/tomcat-servlet-testing-example#85 | ||
// mbland/tomcat-servlet-testing-example@b5df30e | ||
describe.skipIf(globalThis.window)('JsdomPageOpener', () => { | ||
/** @type {JsdomPageOpener} */ | ||
let opener | ||
const fixture = new JsdomFixture() | ||
|
||
beforeAll(async () => {opener = new JsdomPageOpener(await import('jsdom'))}) | ||
|
||
afterEach(() => { | ||
fixture.restoreOrigWindowAndDocument() | ||
vi.restoreAllMocks() | ||
}) | ||
|
||
test('logs error if missing #app div', async () => { | ||
const pagePath = './test-modules/missing.html' | ||
const consoleSpy = vi.spyOn(console, 'error') | ||
.mockImplementationOnce(() => {}) | ||
|
||
const { close } = await opener.open('/basedir/', pagePath) | ||
close() | ||
|
||
expect(consoleSpy).toBeCalledWith('no #app element') | ||
}) | ||
}) |