-
-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Synchronous preprocessing for Svelte TypeScript tests #85
Draft
ehrencrona
wants to merge
2
commits into
lukeed:master
Choose a base branch
from
ehrencrona:sync_preprocess
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"private": true, | ||
"scripts": { | ||
"test": "uvu tests -r esm -r tests/setup/register -i setup" | ||
}, | ||
"devDependencies": { | ||
"cosmiconfig": "^7.0.0", | ||
"esm": "3.2.25", | ||
"jsdom": "16.3.0", | ||
"svelte": "3.24.0", | ||
"svelte-preprocess": "^4.2.1", | ||
"typescript": "^3.9.7", | ||
"uvu": "^0.2.0" | ||
} | ||
} |
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 @@ | ||
<script> | ||
type CountResult = number | ||
export let count: CountResult = 5; | ||
|
||
function increment(): void { | ||
count++; | ||
} | ||
|
||
function decrement(): void { | ||
count--; | ||
} | ||
</script> | ||
|
||
<button id="decr" on:click={decrement}>--</button> | ||
<span>{count}</span> | ||
<button id="incr" on:click={increment}>++</button> |
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,9 @@ | ||
const sveltePreprocess = require("svelte-preprocess"); | ||
|
||
const defaults = { | ||
script: "typescript", | ||
}; | ||
|
||
module.exports = { | ||
preprocess: sveltePreprocess({ defaults }) | ||
} |
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,67 @@ | ||
import { test } from 'uvu'; | ||
import * as assert from 'uvu/assert'; | ||
import * as ENV from './setup/env'; | ||
|
||
// Relies on `setup/register` | ||
import Count from '../src/Count.svelte'; | ||
|
||
test.before(ENV.setup); | ||
test.before.each(ENV.reset); | ||
|
||
test('should render with "5" by default', () => { | ||
const { container } = ENV.render(Count); | ||
|
||
assert.snapshot( | ||
container.innerHTML, | ||
`<button id="decr">--</button> <span>5</span> <button id="incr">++</button>` | ||
); | ||
}); | ||
|
||
test('should accept custom `count` prop', () => { | ||
const { container } = ENV.render(Count, { count: 99 }); | ||
|
||
assert.snapshot( | ||
container.innerHTML, | ||
`<button id="decr">--</button> <span>99</span> <button id="incr">++</button>` | ||
); | ||
}); | ||
|
||
test('should increment count after `button#incr` click', async () => { | ||
const { container } = ENV.render(Count); | ||
|
||
assert.snapshot( | ||
container.innerHTML, | ||
`<button id="decr">--</button> <span>5</span> <button id="incr">++</button>` | ||
); | ||
|
||
await ENV.fire( | ||
container.querySelector('#incr'), | ||
'click' | ||
); | ||
|
||
assert.snapshot( | ||
container.innerHTML, | ||
`<button id="decr">--</button> <span>6</span> <button id="incr">++</button>` | ||
); | ||
}); | ||
|
||
test('should decrement count after `button#decr` click', async () => { | ||
const { container } = ENV.render(Count); | ||
|
||
assert.snapshot( | ||
container.innerHTML, | ||
`<button id="decr">--</button> <span>5</span> <button id="incr">++</button>` | ||
); | ||
|
||
await ENV.fire( | ||
container.querySelector('#decr'), | ||
'click' | ||
); | ||
|
||
assert.snapshot( | ||
container.innerHTML, | ||
`<button id="decr">--</button> <span>4</span> <button id="incr">++</button>` | ||
); | ||
}); | ||
|
||
test.run(); |
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,47 @@ | ||
import { JSDOM } from 'jsdom'; | ||
import { tick } from 'svelte'; | ||
|
||
const { window } = new JSDOM(''); | ||
|
||
export function setup() { | ||
// @ts-ignore | ||
global.window = window; | ||
global.document = window.document; | ||
global.navigator = window.navigator; | ||
global.getComputedStyle = window.getComputedStyle; | ||
global.requestAnimationFrame = null; | ||
} | ||
|
||
export function reset() { | ||
window.document.title = ''; | ||
window.document.head.innerHTML = ''; | ||
window.document.body.innerHTML = ''; | ||
} | ||
|
||
/** | ||
* @typedef RenderOutput | ||
* @property container {HTMLElement} | ||
* @property component {import('svelte').SvelteComponent} | ||
*/ | ||
|
||
/** | ||
* @return {RenderOutput} | ||
*/ | ||
export function render(Tag, props = {}) { | ||
Tag = Tag.default || Tag; | ||
const container = window.document.body; | ||
const component = new Tag({ props, target: container }); | ||
return { container, component }; | ||
} | ||
|
||
/** | ||
* @param {HTMLElement} elem | ||
* @param {String} event | ||
* @param {any} [details] | ||
* @returns Promise<void> | ||
*/ | ||
export function fire(elem, event, details) { | ||
let evt = new window.Event(event, details); | ||
elem.dispatchEvent(evt); | ||
return tick(); | ||
} |
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,47 @@ | ||
const { parse } = require('path'); | ||
const { compile, preprocess_sync } = require('svelte/compiler'); | ||
const { getSvelteConfig } = require('./svelteconfig.js'); | ||
const { cosmiconfigSync } = require('cosmiconfig') | ||
|
||
const useTransformer = (options = {}) => (source, filename) => { | ||
const { preprocess, rootMode } = options; | ||
if (preprocess) { | ||
const svelteConfig = getSvelteConfig(rootMode, filename); | ||
const config = cosmiconfigSync().load(svelteConfig).config | ||
|
||
return preprocess_sync(source, config.preprocess || {}, { filename }).code | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this line is pretty much the only change compared to #49 |
||
} | ||
else { | ||
return source; | ||
} | ||
}; | ||
|
||
function transform(hook, source, filename) { | ||
const { name } = parse(filename); | ||
|
||
const preprocessed = useTransformer({ preprocess: true })(source, filename); | ||
|
||
const {js, warnings} = compile(preprocessed, { | ||
name: name[0].toUpperCase() + name.slice(1), | ||
format: 'cjs', | ||
filename | ||
}); | ||
|
||
warnings.forEach(warning => { | ||
console.warn(`\nSvelte Warning in ${warning.filename}:`); | ||
console.warn(warning.message); | ||
console.warn(warning.frame); | ||
}); | ||
|
||
return hook(js.code, filename); | ||
} | ||
|
||
const loadJS = require.extensions['.js']; | ||
|
||
// Runtime DOM hook for require("*.svelte") files | ||
// Note: for SSR/Node.js hook, use `svelte/register` | ||
require.extensions['.svelte'] = function (mod, filename) { | ||
const orig = mod._compile.bind(mod); | ||
mod._compile = code => transform(orig, code, filename); | ||
loadJS(mod, filename); | ||
} |
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,28 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
const configFilename = 'svelte.config.js' | ||
|
||
exports.getSvelteConfig = (rootMode, filename) => { | ||
const configDir = rootMode === 'upward' | ||
? getConfigDir(path.dirname(filename)) | ||
: process.cwd() | ||
const configFile = path.resolve(configDir, configFilename) | ||
|
||
if (!fs.existsSync(configFile)) { | ||
throw Error(`Could not find ${configFilename}`) | ||
} | ||
|
||
return configFile | ||
} | ||
|
||
const getConfigDir = (searchDir) => { | ||
if (fs.existsSync(path.join(searchDir, configFilename))) { | ||
return searchDir | ||
} | ||
|
||
const parentDir = path.resolve(searchDir, '..') | ||
return parentDir !== searchDir | ||
? getConfigDir(parentDir) | ||
: searchDir // Stop walking at filesystem root | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably unnecessary to load the config for every file to be processed.