Skip to content

Commit 6212457

Browse files
fix: run generated test through temporary file (nightwatchjs#9)
1 parent c210297 commit 6212457

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
.vim
2+
.idea
13
node_modules
24
logs
35
tests_output
6+
7+
# Debug config for Vimspector (Vim/Neovim)
8+
.vimspector.json
9+
10+
# Vim's swap files
11+
*.swp
12+
*.swo

nightwatch/commands/mountReactComponent.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const AssertionError = require('assertion-error');
22
const fs = require('fs');
33
const path = require('path');
44

5+
const {TMP_TEST_NAME, writeTmpTestFile, removeTmpTestFile} = require('../../src/tmp_file.js');
6+
57
class NightwatchMountError extends AssertionError {
68
constructor(message) {
79
super(message);
@@ -46,18 +48,21 @@ module.exports = class Command {
4648
let beforeMountError;
4749
let afterMountError;
4850

51+
await Command._createEntryScriptFile(componentOrName, props, isJSX);
52+
4953
// mount component
5054
await this.api
51-
.execute(function (innerHTML) {
55+
.executeAsyncScript(function (fileName, done) {
5256
function onReady(fn) {if (document.readyState === 'complete' || document.readyState === 'interactive') {setTimeout(fn);} else {document.addEventListener('DOMContentLoaded', fn)}}
5357
onReady(function() {
5458
var scriptTag = Object.assign(document.createElement('script'), {
59+
src: `/${fileName}?t=${Math.random().toString(36)}`,
5560
type: 'module',
56-
innerHTML
61+
onload: function() { done(); }
5762
});
5863
document.body.appendChild(scriptTag);
5964
});
60-
}, [Command._buildScript(componentOrName, props, isJSX)])
65+
}, [TMP_TEST_NAME])
6166

6267
// FIXME: writing the waitUntil outside of the perform using await, breaks the chain
6368

@@ -207,6 +212,8 @@ module.exports = class Command {
207212
done(browserResult);
208213
}, 100);
209214
}, [Command.rootElementId]);
215+
216+
await removeTmpTestFile();
210217

211218
if (!result || beforeMountError || afterMountError) {
212219
const err = this.getError('Could not mount the component.');
@@ -331,6 +338,9 @@ module.exports = class Command {
331338

332339
static _buildScript(Component, props, isJSX) {
333340
return `
341+
// THIS IS GENERATED FILE. DO NOT EDIT IT!
342+
// THIS FILE SHOULD NOT BE ADDED TO THE VERSION CONTROL SYSTEM.
343+
334344
${Command._getReactImports()}
335345
${Command._addDescribeMocks(isJSX)}
336346
@@ -365,4 +375,20 @@ module.exports = class Command {
365375
window.__$$PlayFnDone = false;
366376
`;
367377
}
378+
379+
/**
380+
* Creates a content for the script that will load component and mount
381+
* it into the renderer's canvas. For that purpose the file will be created
382+
* in the current working directory. That file has to be deleted after a testing is done.
383+
*
384+
* @param {string|ComponentDescription|(() => JSX.Element)} componentOrName
385+
* @param {unknown} props
386+
* @param {boolean} isJSX
387+
* @returns {Promise<void>}
388+
*/
389+
static async _createEntryScriptFile(componentOrName, props, isJSX) {
390+
const content = Command._buildScript(componentOrName, props, isJSX);
391+
392+
await writeTmpTestFile(content);
393+
}
368394
};

src/tmp_file.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const fs = require("fs");
2+
3+
const TMP_TEST_FILE = "_tmp_test_file.js";
4+
5+
/**
6+
* Flushes the content to the FS as the virtual test.
7+
*
8+
* @param {string} content
9+
* @returns {Promise<void>}
10+
*/
11+
const write = (content) =>
12+
fs.promises.writeFile(TMP_TEST_FILE, content, {
13+
encoding: "utf8",
14+
});
15+
16+
/**
17+
* Removes the virtual test file from the FS.
18+
*
19+
* @returns {Promise<void>}
20+
*/
21+
const clean = () =>
22+
fs.promises.unlink(TMP_TEST_FILE).catch((error) => {
23+
if (error.code === "ENOENT") {
24+
// The file doesn't exist and it is okay. Just do nothing.
25+
} else {
26+
throw error;
27+
}
28+
});
29+
30+
exports.TMP_TEST_NAME = TMP_TEST_FILE;
31+
exports.writeTmpTestFile = write;
32+
exports.removeTmpTestFile = clean;

0 commit comments

Comments
 (0)