Skip to content
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

Added Option to Automatically Add Vitest to New Projects using svelte-create #5708

Merged
merged 24 commits into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2a35dd1
add vitest option to svelte-create
bertybot Jul 25, 2022
09d4ef3
added changeset
bertybot Jul 25, 2022
5fa5ec0
add c8 package for coverage
bertybot Jul 25, 2022
e4e9ccf
ran format
bertybot Jul 25, 2022
1bf7c59
add vitest option to fix typescript error
bertybot Jul 25, 2022
fdd3fcd
Remove svelte-testing-library
bertybot Jul 25, 2022
e75378f
update to just modify the vite.config.js
bertybot Jul 25, 2022
a168514
readd default vite-config for non vitest projects
bertybot Jul 25, 2022
e9e38a6
remove coverage testing
bertybot Jul 26, 2022
2f97dee
Merge branch 'master' into add-vitest-to-create
Rich-Harris Aug 17, 2022
8c451fe
create typescript/checkjs/no-typechecking variants of .ts files in sh…
Rich-Harris Aug 17, 2022
e48c02f
simplify include
Rich-Harris Aug 17, 2022
f59f9c0
add testMatch to base playwright configs, remove vitest+playwright co…
Rich-Harris Aug 17, 2022
0a8fc37
Update packages/create-svelte/scripts/build-templates.js
benmccann Sep 30, 2022
b512b49
Merge branch 'master' into add-vitest-to-create
benmccann Sep 30, 2022
691e874
Update packages/create-svelte/shared/+playwright+typescript/playwrigh…
benmccann Sep 30, 2022
78149b5
cleanup test patterns
benmccann Sep 30, 2022
c06a74f
newer version of vitest
benmccann Sep 30, 2022
db95545
sverdle test
benmccann Sep 30, 2022
e670e29
format
benmccann Sep 30, 2022
e71e21a
removed vitest globals
bertybot Sep 30, 2022
658dd56
Rename vite.config.ts to vite.config.js
bertybot Oct 3, 2022
8d3d11d
Fixed types on default vite config
bertybot Oct 3, 2022
01ab4ba
Merge remote-tracking branch 'upstream/HEAD' into add-vitest-to-create
bertybot Nov 10, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dirty-teachers-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': patch
---

Added the option to add Vitest to new projects
13 changes: 13 additions & 0 deletions packages/create-svelte/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ async function main() {
initial: false,
active: 'Yes',
inactive: 'No'
},
{
type: 'toggle',
name: 'vitest',
message: 'Add Vitest for unit testing?',
initial: false,
active: 'Yes',
inactive: 'No'
}
],
{
Expand Down Expand Up @@ -150,6 +158,11 @@ async function main() {
console.log(cyan(' https://playwright.dev'));
}

if (options.vitest) {
console.log(bold('✔ Vitest'));
console.log(cyan(' https://vitest.dev'));
}

console.log('\nInstall community-maintained integrations:');
console.log(cyan(' https://github.com/svelte-add/svelte-adders'));

Expand Down
37 changes: 28 additions & 9 deletions packages/create-svelte/scripts/build-templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,22 +206,41 @@ async function generate_shared() {
}

if (name.endsWith('.ts') && !include.includes('typescript')) {
// file includes types in TypeScript and JSDoc —
// create .js file, with and without JSDoc
benmccann marked this conversation as resolved.
Show resolved Hide resolved
const js = convert_typescript(contents);
const js_name = name.replace(/\.ts$/, '.js');
shared.add(js_name);

// typescript
files.push({
name: js_name,
include: [...include],
exclude: [...exclude, 'typescript'],
contents: convert_typescript(contents)
name,
include: [...include, 'typescript'],
exclude,
contents: strip_jsdoc(contents)
});

include.push('typescript');
}
// checkjs
files.push({
name: js_name,
include: [...include, 'checkjs'],
exclude,
contents: js
});

shared.add(name);
// no typechecking
files.push({
name: js_name,
include,
exclude: [...exclude, 'typescript', 'checkjs'],
contents: strip_jsdoc(js)
});

files.push({ name, include, exclude, contents });
shared.add(name);
shared.add(js_name);
} else {
shared.add(name);
files.push({ name, include, exclude, contents });
}
});

files.sort((a, b) => a.include.length + a.exclude.length - (b.include.length + b.exclude.length));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ await create(repo, {
eslint: false,
types: 'checkjs',
prettier: true,
playwright: false
playwright: false,
vitest: false
});
14 changes: 14 additions & 0 deletions packages/create-svelte/shared/+vitest+checkjs/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"types": ["vitest/globals"],
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { PlaywrightTestConfig } from '@playwright/test';

const config: PlaywrightTestConfig = {
webServer: {
command: 'npm run build && npm run preview',
port: 4173
},
testMatch: 'tests/**/*.{js,ts}'
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
};

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
webServer: {
command: 'npm run build && npm run preview',
port: 4173
},
testMatch: 'tests/**/*.js'
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
};

export default config;
14 changes: 14 additions & 0 deletions packages/create-svelte/shared/+vitest+typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"types": ["vitest/globals"],
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true
}
}
8 changes: 8 additions & 0 deletions packages/create-svelte/shared/+vitest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"devDependencies": {
"vitest": "^0.19.1"
},
"scripts": {
"test:unit": "vitest"
}
}
7 changes: 7 additions & 0 deletions packages/create-svelte/shared/+vitest/src/lib/sum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { sum } from './sum';
benmccann marked this conversation as resolved.
Show resolved Hide resolved

describe('sum test', () => {
it('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
});
8 changes: 8 additions & 0 deletions packages/create-svelte/shared/+vitest/src/lib/sum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Add two numbers
* @param {number} a
* @param {number} b
*/
export function sum(a: number, b: number): number {
benmccann marked this conversation as resolved.
Show resolved Hide resolved
return a + b;
}
12 changes: 12 additions & 0 deletions packages/create-svelte/shared/+vitest/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { sveltekit } from '@sveltejs/kit/vite';

/** @type {import('vite').UserConfig} */
const config = {
plugins: [sveltekit()],
test: {
include: ['src/**/*.{test}.{js,ts}'],
benmccann marked this conversation as resolved.
Show resolved Hide resolved
benmccann marked this conversation as resolved.
Show resolved Hide resolved
globals: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another discussion ;) globals: true would mean we'd have to also provide additional env typing for the test files, just leave it off and resort to imports? It's cleaner anyways

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, just gonna admit that a lot of my decisions, have been based off what I've seen other people doing, and what my team has been doing.

I took a lot of inspiration from @davipon on his add vitest library
https://github.com/davipon/svelte-add-vitest/blob/main/templates/config/vite.config.js

I have yet to see someone not use globals I think because having to add them for every single test starts to get annoying, adding the imports just becomes repetitive boilerplate after a while. Definitely a preference though, the script already adds the vitest types to the tsconfig or Jsconfig based on what you choose.

}
};

export default config;
8 changes: 8 additions & 0 deletions packages/create-svelte/shared/-vitest/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { sveltekit } from '@sveltejs/kit/vite';

/** @type {import('vite').UserConfig} */
const config = {
plugins: [sveltekit()]
};

export default config;
2 changes: 2 additions & 0 deletions packages/create-svelte/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type Options = {
prettier: boolean;
eslint: boolean;
playwright: boolean;
vitest: boolean;
};

export type File = {
Expand All @@ -18,6 +19,7 @@ export type Condition =
| 'typescript'
| 'checkjs'
| 'playwright'
| 'vitest'
| 'skeleton'
| 'default';

Expand Down