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 all 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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ await create(repo, {
eslint: false,
types: 'checkjs',
prettier: true,
playwright: false
playwright: false,
vitest: false
});

// Remove the Sverdle from the template because it doesn't work within Stackblitz (cookies not set)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, it, expect } from 'vitest';
import { Game } from './game';

describe('game test', () => {
it('returns true when a valid word is entered', () => {
const game = new Game();
expect(game.enter('zorro'.split(''))).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { expect, test } from '@playwright/test';

test('index page has expected h1', async ({ page }) => {
await page.goto('/');
expect(await page.textContent('h1')).toBe('Welcome to SvelteKit');
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const config: PlaywrightTestConfig = {
webServer: {
command: 'npm run build && npm run preview',
port: 4173
}
},
testMatch: 'tests/**/.*(test|spec).(js|ts)'
};

export default config;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const config = {
webServer: {
command: 'npm run build && npm run preview',
port: 4173
}
},
testMatch: 'tests/**/.*(test|spec).(js|ts)'
};

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { describe, it, expect } from 'vitest';

describe('sum test', () => {
it('adds 1 + 2 to equal 3', () => {
expect(1 + 2).toBe(3);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { describe, it, expect } from 'vitest';

describe('sum test', () => {
it('adds 1 + 2 to equal 3', () => {
expect(1 + 2).toBe(3);
});
});
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.23.4"
},
"scripts": {
"test:unit": "vitest"
}
}
11 changes: 11 additions & 0 deletions packages/create-svelte/shared/+vitest/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { sveltekit } from '@sveltejs/kit/vite';

/** @type {import('vite').UserConfig} */
const config = {
plugins: [sveltekit()],
test: {
include: ['src/**/*.{test,spec}.{js,ts}']
Copy link

@jorgebv jorgebv Nov 24, 2022

Choose a reason for hiding this comment

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

One late note since I saw the topic of globals was already discussed:

Some libraries enable features automatically based on these globals and users coming from jest may be confused as to why things aren’t working the same way out of the box. One that got me was Testing Library, which enables auto cleanup only if the global is present. https://testing-library.com/docs/react-testing-library/api#cleanup (this is a link to react docs but it applies equally to the svelte version of the library)

I personally have globals enabled but I have a second tsconfig to add the global types to my testing folder, which I saw you are trying to avoid. I don’t think there’s one true answer here but just wanted to leave this note in case it benefits anyone.

}
};

export default config;
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { sveltekit } from '@sveltejs/kit/vite';
import type { UserConfig } from 'vite';

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { invalid } from '@sveltejs/kit';
import { words, allowed } from './words.server';
import { Game } from './game';
import type { PageServerLoad, Actions } from './$types';

/** @type {import('./$types').PageServerLoad} */
Expand Down Expand Up @@ -69,79 +69,3 @@ export const actions: Actions = {
cookies.delete('sverdle');
}
};

class Game {
index: number;
guesses: string[];
answers: string[];
answer: string;

/**
* Create a game object from the player's cookie, or initialise a new game
* @param {string | undefined} serialized
*/
constructor(serialized: string | undefined) {
if (serialized) {
const [index, guesses, answers] = serialized.split('-');

this.index = +index;
this.guesses = guesses ? guesses.split(' ') : [];
this.answers = answers ? answers.split(' ') : [];
} else {
this.index = Math.floor(Math.random() * words.length);
this.guesses = ['', '', '', '', '', ''];
this.answers = /** @type {string[]} */ [] /***/;
}

this.answer = words[this.index];
}

/**
* Update game state based on a guess of a five-letter word. Returns
* true if the guess was valid, false otherwise
* @param {string[]} letters
*/
enter(letters: string[]) {
const word = letters.join('');
const valid = allowed.has(word);

if (!valid) return false;

this.guesses[this.answers.length] = word;

const available = Array.from(this.answer);
const answer = Array(5).fill('_');

// first, find exact matches
for (let i = 0; i < 5; i += 1) {
if (letters[i] === available[i]) {
answer[i] = 'x';
available[i] = ' ';
}
}

// then find close matches (this has to happen
// in a second step, otherwise an early close
// match can prevent a later exact match)
for (let i = 0; i < 5; i += 1) {
if (answer[i] === '_') {
const index = available.indexOf(letters[i]);
if (index !== -1) {
answer[i] = 'c';
available[index] = ' ';
}
}
}

this.answers.push(answer.join(''));

return true;
}

/**
* Serialize game state so it can be set as a cookie
*/
toString() {
return `${this.index}-${this.guesses.join(' ')}-${this.answers.join(' ')}`;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { words, allowed } from './words.server';

export class Game {
index: number;
guesses: string[];
answers: string[];
answer: string;

/**
* Create a game object from the player's cookie, or initialise a new game
* @param {string | undefined} serialized
*/
constructor(serialized: string | undefined = undefined) {
if (serialized) {
const [index, guesses, answers] = serialized.split('-');

this.index = +index;
this.guesses = guesses ? guesses.split(' ') : [];
this.answers = answers ? answers.split(' ') : [];
} else {
this.index = Math.floor(Math.random() * words.length);
this.guesses = ['', '', '', '', '', ''];
this.answers = /** @type {string[]} */ [] /***/;
}

this.answer = words[this.index];
}

/**
* Update game state based on a guess of a five-letter word. Returns
* true if the guess was valid, false otherwise
* @param {string[]} letters
*/
enter(letters: string[]) {
const word = letters.join('');
const valid = allowed.has(word);

if (!valid) return false;

this.guesses[this.answers.length] = word;

const available = Array.from(this.answer);
const answer = Array(5).fill('_');

// first, find exact matches
for (let i = 0; i < 5; i += 1) {
if (letters[i] === available[i]) {
answer[i] = 'x';
available[i] = ' ';
}
}

// then find close matches (this has to happen
// in a second step, otherwise an early close
// match can prevent a later exact match)
for (let i = 0; i < 5; i += 1) {
if (answer[i] === '_') {
const index = available.indexOf(letters[i]);
if (index !== -1) {
answer[i] = 'c';
available[index] = ' ';
}
}
}

this.answers.push(answer.join(''));

return true;
}

/**
* Serialize game state so it can be set as a cookie
*/
toString() {
return `${this.index}-${this.guesses.join(' ')}-${this.answers.join(' ')}`;
}
}
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'
| 'skeletonlib';
Expand Down