forked from sveltejs/kit
-
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.
Template tests POC using @web/test-runner (sveltejs#19)
- Loading branch information
Showing
4 changed files
with
109 additions
and
1 deletion.
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
16 changes: 16 additions & 0 deletions
16
packages/create-svelte/templates/default/src/test/Counter.test.js
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,16 @@ | ||
import { strict as assert } from 'assert'; | ||
import Counter from '$lib/Counter/index.svelte'; | ||
|
||
describe('Counter', () => { | ||
it('should increment when plus button is clicked', async () => { | ||
const target = document.getElementById('svelte'); | ||
new Counter({ target }); | ||
const plusButton = target.getElementsByTagName('button')[1]; | ||
const counter = target.querySelectorAll('.counter-digits strong')[1]; | ||
assert.equal(counter.innerText, '0'); | ||
plusButton.click(); | ||
// you probably want to use fake timers in your real tests! | ||
await new Promise((resolve) => setTimeout(resolve, 500)); | ||
assert.equal(counter.innerText, '1'); | ||
}); | ||
}); |
10 changes: 10 additions & 0 deletions
10
packages/create-svelte/templates/default/src/test/HomePage.test.js
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,10 @@ | ||
import { strict as assert } from 'assert'; | ||
import HomePage from '../routes/index.svelte'; | ||
|
||
describe('HomePage', () => { | ||
it('should render', () => { | ||
const target = document.getElementById('svelte'); | ||
new HomePage({ target }); | ||
assert(/try editing src\/routes\/index.svelte/.test(target.innerText)); | ||
}); | ||
}); |
79 changes: 79 additions & 0 deletions
79
packages/create-svelte/templates/default/web-test-runner.config.js
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,79 @@ | ||
import { spawn } from 'child_process'; | ||
|
||
/* | ||
* Implementing plugin inline as a POC. This would be extracted to a separate | ||
* npm module following a pattern similar to @snowpack/web-test-runner-plugin: | ||
* https://github.com/snowpackjs/snowpack/tree/main/plugins/web-test-runner-plugin | ||
* or vite-web-test-runner-plugin: | ||
* https://github.com/betaboon/vite-web-test-runner-plugin | ||
*/ | ||
const svelteKitPlugin = function () { | ||
let server; | ||
|
||
// quick hack; use more resilient approach in real plugin | ||
function randomPort(min = 49152, max = 65535) { | ||
return Math.floor(Math.random() * (max - min) + min); | ||
} | ||
|
||
// Currently, svelte-kit does not expose a method to the outside world to | ||
// start a dev server in JS-land. Work-around for POC: spawn a child process | ||
// that shells-out to svelte-kit cli. For real plugin, extract and export a | ||
// method from svelte-kit to start a dev server. | ||
function startTestServer(port) { | ||
const testServer = spawn('npx', ['svelte-kit', 'dev', `--port=${port}`]); | ||
|
||
testServer.stderr.on('data', (data) => { | ||
console.error(data.toString()); | ||
}); | ||
|
||
testServer.stdout.on('data', (data) => { | ||
console.log(data.toString()); | ||
}); | ||
|
||
return new Promise((resolve, reject) => { | ||
testServer.stdout.on('data', (data) => { | ||
if (/http:\/\/localhost:\d+/.test(data)) { | ||
resolve(testServer); | ||
} | ||
}); | ||
|
||
testServer.on('close', reject); | ||
}); | ||
} | ||
|
||
return { | ||
name: 'svelte-kit-plugin', | ||
|
||
async serverStart({ app }) { | ||
const port = randomPort(); | ||
server = await startTestServer(port); | ||
app.use((ctx, next) => { | ||
ctx.redirect(`http://localhost:${port}${ctx.originalUrl}`); | ||
}); | ||
}, | ||
|
||
async serverStop() { | ||
return server.kill(); | ||
} | ||
}; | ||
}; | ||
|
||
process.env.NODE_ENV = 'test'; | ||
|
||
export default { | ||
plugins: [svelteKitPlugin()], | ||
testRunnerHtml: (testFramework) => ` | ||
<html> | ||
<head> | ||
<script> | ||
global = window; | ||
process = { env: { NODE_ENV: "test" } }; | ||
</script> | ||
<script type="module" src="${testFramework}"></script> | ||
</head> | ||
<body> | ||
<div id="svelte"></div> | ||
</body> | ||
</html> | ||
` | ||
}; |