Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Svelte with Test-Driven Development
Svelte with Test-Driven Development

Svelte with Test-Driven Development: Advance your skills and write effective automated tests with Vitest, Playwright, and Cucumber.js

Arrow left icon
Profile Icon Daniel Irvine
Arrow right icon
Mex$401.99 Mex$574.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.9 (10 Ratings)
eBook Jun 2023 250 pages 1st Edition
eBook
Mex$401.99 Mex$574.99
Paperback
Mex$717.99
Subscription
Free Trial
Arrow left icon
Profile Icon Daniel Irvine
Arrow right icon
Mex$401.99 Mex$574.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.9 (10 Ratings)
eBook Jun 2023 250 pages 1st Edition
eBook
Mex$401.99 Mex$574.99
Paperback
Mex$717.99
Subscription
Free Trial
eBook
Mex$401.99 Mex$574.99
Paperback
Mex$717.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Svelte with Test-Driven Development

Setting up for Testing

Back when you were a young schoolchild, you probably learned to write by using a pencil on paper. Now that you’re older, it’s likely you prefer pens. For learners, pencils have a distinct advantage over pens in that mistakes are easy to correct, and when you first start writing out letters and words, you will be making a lot of mistakes. Pencils are also safer for small children – no caps or messy ink to worry about.

But pencils remain a valid writing instrument, and you might still have a personal preference for pencils over pens. The pencil is a perfectly good tool for the job.

Test-Driven Development (TDD) is a tool that can serve you in a similar way. It’s a great way to learn and grow as a developer. Many experienced developers prefer it for their day-to-day work over any alternative.

In this chapter, you’ll configure a work environment that’s designed to help you get the most out of TDD techniques. Since TDD asks you to do a bunch of small repetitive tasks – writing tests, running tests, committing early and often, and switching between test code and application code – it’s important that each of those tasks can be done easily and quickly.

It follows that an important personal discipline to cultivate is that of objectively critiquing your development tools. For every tool that you use, ask yourself this: is this tool serving me well? Is it easy and quick to use?

This could be your Integrated Development Environment (IDE), your operating system, your source code repository, your note-taking program, your time management utilities, and so on. Anything and everything you use in your day job. Scrutinize your tools. Throw away whatever isn’t working for you.

This is a very personal thing and depends a lot on experience and individuality. And your preferences are likely to change over time, too.

I often reach for very plain, simple, keyboard-driven tools that work for me consistently, regardless of the programming language I’m working in, such as the text editor Vim. It doesn’t offer any knowledge about the JavaScript programming language or the Svelte framework, but it makes me extremely effective at editing text.

But if you care about learning JavaScript or program design, then you might prefer an IDE that gives you JavaScript auto-complete suggestions and helpful project assistance.

This chapter walks through the setup of a new SvelteKit project and highlights all the individual choices you’ll need to make, and the additional extras you’ll need in order to practice effective TDD.

It covers the following topics:

  • Creating a new SvelteKit project
  • Preparing your development environment for frequent test runs
  • Configuring support for Svelte component tests
  • Optional configuration you may want to try

By the end of the chapter, you’ll know how to create a new Svelte project that is ready for test-driven feature building.

Technical requirements

The code for the chapter can be found online at https://github.com/PacktPublishing/Svelte-with-Test-Driven-Development/tree/main/Chapter01/Start.

You will need to have a recent version of Node.js installed. See https://nodejs.org for instructions on how to install and update Node.js for your platform.

Creating a new SvelteKit project

In this section, you’ll use the default method for creating a new SvelteKit project, which uses the npm create command. (For reference, you can also check the official documentation at https://kit.svelte.dev/docs/creating-a-project.)

The project we are building is called Birthdays and the npm package name is birthdays. It will be introduced properly in Chapter 2, Introducing the Red-Green-Refactor Workflow.

SvelteKit 1.0

These instructions were valid at the time of writing, for SvelteKit 1.0. It’s likely things will improve in time, so you may find some of the later instructions will become unnecessary or may no longer work. Check the book’s GitHub repository for the most up-to-date instructions.

For now, we’ll concentrate on the mechanics of building a new project:

  1. Start by opening a Terminal window in your usual work location (for me, this is ~/work on my Mac). Then type the following commands:
    mkdir birthdays
    cd birthdays
    npm create svelte@latest

If this is the first Svelte project you’ve created, you’ll be presented with the following message from npm:

Need to install the following packages:
  [email protected]
Ok to proceed? (y)
  1. Answer y to that. You’ll see a bunch more questions, which we’ll go through individually:
    create-svelte version 2.1.0
    Welcome to SvelteKit!
    ? Where should we create your project?
      (leave blank to use current directory) ›
  2. Since you’re already in the birthdays directory, just leave this blank, and hit Enter. Next, you’ll be asked about which app template you’d like to use:
    ? Which Svelte app template? › - Use arrow-keys. Return to submit.
        SvelteKit demo app
       Skeleton project - Barebones scaffolding for your new SvelteKit app
        Library skeleton project
  3. Choose Skeleton project. Next, you’ll be asked about TypeScript:
    ? Add type checking with TypeScript? › - Use arrow-keys. Return to submit.
        Yes, using JavaScript with JSDoc comments
        Yes, using TypeScript syntax
       No
  4. For this question, I’ve chosen No. That’s because this book is about testing techniques, not typing techniques. That’s not to say that this book doesn’t apply to TypeScript projects – it most certainly does – just that typing is not the topic at hand.

If you want to use TypeScript

If you’re a seasoned TypeScript developer, please feel free to choose that option instead. The code samples in the book won’t need too much modification except for the additional type definitions, which you’ll need to provide.

  1. Finally, you’ll be asked about extra package dependencies:
    ? Add ESLint for code linting? › No / Yes
    ? Add Prettier for code formatting? › No / Yes
    ? Add Playwright for browser testing? › No / Yes
         Add Vitest for unit testing? … No / Yes
  2. Choose Yes as the answer to all these questions. Although we won’t mention ESLint in this book, it’s always good to have. And we’ll need Playwright and Vitest.

You’ll then see a summary of all your choices, followed by a Next steps list:

Your project is ready!
 ESLint
  https://github.com/sveltejs/eslint-plugin-svelte3
 Prettier
  https://prettier.io/docs/en/options.html
  https://github.com/sveltejs/prettier-plugin-svelte#options
 Playwright
  https://playwright.dev
 Vitest
  https://vitest.dev
Install community-maintained integrations:
  https://github.com/svelte-add/svelte-adders
Next steps:
  1: npm install (or pnpm install, etc)
  2: git init && git add -A && git commit -m "Initial commit" (optional)
  3: npm run dev -- --open

We’ll perform these next steps but before we do that, we’ll run some extra verification steps. It’s always good to check your work.

Type npm install into the Terminal and confirm that everything is installed correctly. Then, go ahead and commit your changes. (If you’ve forked the GitHub repository, you won’t need to use the git init command.)

Committing early and often

It’s a good idea to be checking in your work very often. While you’re learning the TDD approach to testing, it can be helpful to check in after every single test. This might seem like a lot but it will help you backtrack in case you get stuck.

Then, run npm run dev – –open. It should open your web browser and show you a "Welcome to SvelteKit" message.

You can then close the browser and hit Ctrl + C in the Terminal to stop the web server.

Next, let’s verify the Playwright and Vitest dependencies.

Installing and running Playwright

Although we won’t use Playwright in this chapter, it’s a good idea to get it installed and verify that it’s working.

Start by running npm test at the command line:

work/birthdays % npm test
> [email protected] test
> playwright test
Running 1 test using 1 worker
[WebServer]
[WebServer]
[WebServer] Generated an empty chunk: "hooks".
[WebServer]
  ✘  1 test.js:3:1 › index page has expected h1 (7ms)
  1) test.js:3:1 › index page has expected h1 =============================================
    browserType.launch: Executable doesn't exist at /Users/daniel/Library/Caches/ms-playwright/chromium-1041/chrome-mac/Chromium.app/Contents/MacOS/Chromium
    ...
  1 failed
    test.js:3:1 › index page has expected h1 ============================================

If you’ve never installed Playwright before, you’ll see a message like the preceding one.

Playwright has its own environment setup to do, such as installing Chromium onto your machine. You can install it with the following command:

npx playwright install

Then, trying npm test again should give you the following output, showing that the one example test that’s included is passing:

> [email protected] test
> playwright test
Running 1 test using 1 worker
[WebServer]
[WebServer]
[WebServer] Generated an empty chunk: "hooks".
[WebServer]
  ✓  1 test.js:3:1 › index page has expected h1 (307ms)
  1 passed (4s)

This test, index page has expected h1, is a test for the "Welcome to SvelteKit" message you saw earlier when you launched the application in the browser.

Running Vitest

Running npm run test:unit is the default way to run Vitest tests. Try it now:

work/birthdays % npm run test:unit
> [email protected] test:unit
> vitest
 DEV  v0.25.8 /Users/daniel/work/birthdays
 ✓ src/index.test.js (1)
 Test Files  1 passed (1)
      Tests  1 passed (1)
   Start at  15:56:18
   Duration  737ms (transform 321ms, setup 0ms, collect 16ms, tests 2ms)
 PASS  Waiting for file changes...
       press h to show help, press q to quit

This automatically puts you in watch mode, which means any changes to the filesystem will cause tests to re-run. Press q to quit this mode. I personally don’t use watch mode and we won’t be using it in this book. See the Creating a shell alias section for a little discussion on why this is.

In the next section, we’ll make the ergonomics of the project a little easier for us.

Preparing your development environment for frequent unit testing

In this section, we’ll take some configuration actions that will make our test-driven lives much simpler.

Choosing your editor

Let’s start with your choice of code editor. More than likely, this means a choice between an IDE, such as Visual Studio Code, or a plain text editor, such as Vim or Emacs.

IDEs tend to have lots of bells and whistles and one of those is the built-in test runner, which runs tests for you and integrates test output into the editor itself. On the other hand, plain text editors will require you to have a separate Terminal window for you to enter test commands directly, as you did in the previous section.

Figure 1.1 shows how my own setup looks, using Vim and tmux to split windows. The top half of the screen is where I edit my source files, and when I’m ready to run tests, I can switch to the bottom half and enter the test command.

Figure 1.1 – Using tmux and Vim

Figure 1.1 – Using tmux and Vim

Figure 1.2 shows the same project in Visual Studio Code with the Vitest extension installed. Notice the test runner has a bunch of neat features, such as the ability to filter the test output, and green ticks next to the line numbers of passing tests.

Figure 1.2 – Using Visual Studio Code to run tests

Figure 1.2 – Using Visual Studio Code to run tests

I think there is a lot to learn from using a plain editor and Terminal setup, but if you don’t feel comfortable with that, then it’s best to stick to your favorite IDE for now.

The one thing you want to make sure of is that it’s easy and quick to run tests. So, if you’re writing a new test, you want to immediately run it and see it fail. And if you’re making a test pass or refactoring tests, make sure you can quickly re-run tests to check your progress.

Creating a shell alias

If you’re choosing to use the Terminal to run tests, then you will almost certainly want to set up an alias to make it simpler to run Vitest unit tests. You’ll recall that there are two commands that you use for running tests: npm test for Playwright tests and the npm run test:unit command for Vitest unit tests.

The style of testing shown in this book follows the classic test pyramid approach to testing, which states that we should have lots of little unit tests (in Vitest) and far fewer system tests (in Playwright).

So, given that we’ll be working much more frequently with Vitest, doesn’t it make sense to have the shorter test command be the one that runs unit tests?

The solution that I use is a shell alias, v, that invokes Vitest. If you wanted to use the standard watch mode, you’d set up the shell alias to run this command:

npx vitest

However, because I don’t want to use watch mode, I set it up to use this command:

npx vitest run

I‘d suggest you use this version, at least while you read through this book. I find that watch mode tends to break silently, especially when you’re in the first stages of setting up a project. To avoid confusion, better to just invoke the test command when you’re ready.

On my Mac, my default shell is zsh, which configures its shell aliases in the ~/.zshrc file. You can add that alias to the file using the following commands:

echo 'alias v="npx vitest run"' >> ~/.zshrc
source ~/.zshrc

Now, you can simply type the v command to run your Vitest unit tests. You can also use this to run a single test file, like this:

v src/index.tests.js

This is a handy way to run just a small part of your test suite.

Changing the test runner to report each test name

Recall that when we ran our Vitest unit tests, the test report told us the filename of the test suite that was run, together with some summary information:

 DEV  v0.25.8 /Users/daniel/work/birthdays
 ✓ src/index.test.js (1)
 Test Files  1 passed (1)
      Tests  1 passed (1)
   Start at  15:56:18
   Duration  737ms (transform 321ms, setup 0ms, collect 16ms, tests 2ms)

It turns out this isn’t enough – we want to see test names too, just like how the Playwright test told us the description of the test that was passing.

Open the vite.config.js file and add a new reporter property that is set to verbose, as shown in the following code block:

 const config = {
   plugins: [sveltekit()],
   test: {
     ...,
     reporter: 'verbose'
   }
 };

Be careful

If you had left your test runner running in watch mode, you’ll need to restart it at this point, and at any other point in which you modify the configuration.

Now, running tests at the command line using the v command will give this:

 RUN  v0.25.8 /Users/daniel/work/birthdays
 ✓ src/index.test.js (1)
   ✓ sum test (1)
     ✓ adds 1 + 2 to equal 3
 Test Files  1 passed (1)
      Tests  1 passed (1)
   Start at  11:02:05
   Duration  905ms (transform 320ms, setup 1ms, collect 16ms, tests 2ms)

Much better!

Watching the test fail

We’re almost done with configuring Vitest, but before continuing, let’s check that the test actually tests what we want it to test. This is an important concept with TDD: if you’ve never seen a test fail, how do you know it tests the right thing?

Open src/index.test.js and take a look:

import { describe, it, expect } from 'vitest';
describe('sum test', () => {
  it('adds 1 + 2 to equal 3', () => {
    expect(1 + 2).toBe(3);
  });
});

Make a change to the expect statement, like the one shown here:

expect(2 + 2).toBe(3);

Now if you run the test, you should see a failure:

 src/index.test.js:5:17
      3| describe('sum test', () => {
      4|  it('adds 1 + 2 to equal 3', () => {
      5|   expect(2 + 2).toBe(3);
       |                 ^
      6|  });
      7| });
  - Expected   "3"
  + Received   "4"

Brilliant – our test runner seems to be in working order. You can go ahead and undo the change to the test, and watch it go green again. That’s it for the basic editor configuration.

Test file location – src or test?

In many other programming environments, test files are kept apart from application source files. A separate directory named something like tests or specs is used to house all executable test scripts.

There can be a couple of advantages to that. First, it can avoid packaging tests with application code when it comes to building deployable units. However, Svelte (and JavaScript in general) doesn’t suffer from this problem because only modules referenced by the application entry point will be bundled.

Second, having a separate directory avoids the mindset of one test file per module. Not all modules need unit tests: if a unit exists as a part of a larger unit, we’ll often just write tests for the top-level unit and those tests will also provide coverage for the lower-level unit. Conversely, sometimes it’s helpful to have two (or more!) test files for a single module.

That’s especially true when using component mocks that wipe out a component mock for an entire module. You might want a test file that mocks a component, and another test file where the component isn’t mocked. We’ll look at component mocks in Chapter 12, Using Component Mocks to Clarify Tests.

The current SvelteKit approach is to keep Vitest test files housed within the src directory. Partly, this is to avoid confusion with Playwright tests, which do live in a separate directory, named tests. (We’ll see Playwright tests starting from Chapter 3, Loading Data into a Route).

This book continues with that style, but I would encourage you to explore and adopt whichever style you feel most comfortable with.

In the next section, we’ll add support for the kinds of tests we’ll be writing throughout the book.

Configuring support for Svelte component tests

A Svelte component test is one that, perhaps unsurprisingly, tests a Svelte component. For this, we need access to a Document Object Model (DOM), which isn’t part of the standard Node.js environment. We’ll also need some additional packages for writing unit test expectations against the DOM.

Installing jsdom and testing library helpers

At the Terminal, run the following command to install the jsdom package and @testing-library packages that we’ll use in our unit tests:

npm install --save-dev \
  jsdom \
  @testing-library/svelte \
  @testing-library/jest-dom \
  @testing-library/user-event

If you’re using TypeScript, at this point, you may wish to add packages containing type definitions.

Next, create a new file named src/vitest/registerMatchers.js with the following content. It ensures that the matchers we’ll be using are available for use via the expect function:

import matchers from '@testing-library/jest-dom/matchers';
import { expect } from 'vitest';
expect.extend(matchers);

Then, update vite.config.js to add a new environment property, which installs jsdom correctly, and also a setupFiles property, which ensures the file defined previously is loaded (and invoked) just before the test suites are loaded:

 const config = {
   plugins: [sveltekit()],
   test: {
     ...,
     reporter: 'verbose',
     environment: 'jsdom',
     setupFiles: ['./src/vitest/registerMatchers.js']
   }
 };

That’s it for the basic setup. Now let’s test it out.

Writing a test for the DOM

Open the src/index.test.js file and add the following test definition, inside the describe block. This test makes use of the document object that is created for us by the jsdom package, and the toHaveTextContent matcher that is provided by the @testing-library/jest-dom package:

it('renders hello into the document', () => {
  document.body.innerHTML =
    '<h1>Hello, world!</h1>';
  expect(document.body).toHaveTextContent(
    'Hello, world!'
  );
});

Now, if you run the test, you should see it pass. But, just as you did with the first test, it’s important to confirm the test actually tests what it says it does. Change the test by commenting out or deleting the first line of the test, and then re-running the test runner.

You should see an output as follows:

 FAIL  src/index.test.js > sum test > renders hello into the document
Error: expect(element).toHaveTextContent()
Expected element to have text content:
  Hello, world!
Received:
  src/index.test.js:9:25
      7|
      8|  it('renders hello into the document', () => {
      9|   expect(document.body).toHaveTextContent(
       |                         ^
     10|    'Hello, world!'
     11|   );

That proves the test is working. You can go ahead and undo the breaking change you made.

Writing a first Svelte component test

Next, let’s write an actual Svelte component and test that out. Create a new file named src/Hello.svelte with the following content:

<script>
  export let name;
</script>
<p>Hello, {name}!</p>

Then, go back to the src/index.test.js file and refactor your test to use this new component. To do that, replace the call to document.outerHTML with a call to the render function, like this:

it('renders hello into the document', () => {
  render(Hello, { name: 'world' });
  expect(document.body).toHaveTextContent(
    'Hello, world!'
  );
});

This render function comes from the @testing-library/svelte package. Import that now, along with an import for the Hello component, placed at the top of the file:

import { render } from '@testing-library/svelte';
import Hello from './Hello.svelte';

Check that the test still passes with the refactor.

Then, add this third test, which verifies that the name prop in the component is being used to verify the output:

it('renders hello, svelte', () => {
  render(Hello, { name: 'Svelte' });
  expect(document.body).toHaveTextContent(
    'Hello, Svelte!'
  );
});

Run the test and make sure it passes.

Now, go ahead and comment out the render call in the last test. You might think that the test fails with an error saying nothing was rendered on-screen. But let’s see what happens:

Error: expect(element).toHaveTextContent()
Expected element to have text content:
  Hello, Svelte!
Received:
  Hello, world!

Hold on a second; is this what we expected? This test didn’t ever print out a Hello, world! message so why is the test expectation picking it up?

It turns out that our tests share the same document object, which is clearly not good for test independence. Imagine if the second test also expected to see Hello, world! rather than Hello, Svelte!. It would have passed by virtue of the first test running. We need to do something about this.

Ensuring the DOM is cleared after each test run

We want to make sure that every test gets its own clean version of the DOM. We can do this by using the cleanup function.

Create a new file named src/vitest/cleanupDom.js:

import { afterEach } from 'vitest';
import { cleanup } from '@testing-library/svelte';
afterEach(cleanup);

Then, insert that into the setupFiles property in vite.config.js:

const config = {
  ...,
  test: {
    ...,
    setupFiles: [
      './src/vitest/cleanupDom.js',
      './src/vitest/registerMatchers.js'
    ]
  }
};

Now, if you run your failing test again, you should see that the Hello, world! message no longer appears.

Before continuing, uncomment the render call and check your tests are back in an all-green state.

Restoring mocks automatically

There’s one final piece of configuration we need in vite.config.js. Add the restoreMocks property, as shown here:

const config = {
  ...,
  test: {
    ...,
    restoreMocks: true
  }
};

This is also important for test independence and will be important in Chapter 11, Replacing Behavior with a Side-By-Side Implementation, when we begin using the vi.fn function for building test doubles.

That covers all the configuration you need for the rest of the book. The next section touches briefly on some optional configurations you might want to consider.

Optional configuration

In this section, we’ll look at configuring Prettier and setting up more appropriate tab widths on the Terminal. These settings mirror the print settings that are used in this book.

Configuring Prettier’s print width

Due to the constraint of the physical pages in this book, I have set the printWidth setting of Prettier to 54 characters, and all code samples are formatted with that setting.

I also think the default value, 100, is too high. I like short columns of text as I find them easier to share and read in all sorts of environments – such as on mobile devices, where it’s much easier to scroll vertically than it is horizontally.

Also, having extra vertical space comes in handy when you are pairing with other developers and you want to refer to particular line numbers (assuming you have line numbers turned on).

In .prettierrc, you can set the print width with the following addition:

{
  "printWidth": 54,
  ...
}

You might be more comfortable with something in the 60 to 80 range.

Reducing the tab width in the Terminal

The Svelte community has a preference for tabs over spaces because tabs are better for screen readers. Unfortunately, a lot of Terminals and shell programs are set up for a default tab width of eight characters, which is way too many for my liking.

Although every Terminal is different, the one solid piece of advice I have is to set git config to use less as its pager, with tab stops at positions 1, 3, 5, and 7:

git config --global core.pager 'less -x1,3,5,7'

This makes git diff and git show much more bearable, and these are two commands I use extremely frequently.

Summary

This chapter has taken a detailed look at the various parts of a base SvelteKit project, showing how Playwright and Vitest are added, together with the additional dependencies you’ll need to write Svelte component tests.

We’ve also looked at some of the ways you can set up your development environment to help you be productive.

You’re now ready to start exploring TDD practices, starting with the Red-Green-Refactor Cycle -> Workflow in the next chapter.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Understand and master the test-driven development (TDD) workflow
  • Explore the principles of unit testing with Vitest and end-to-end testing using Playwright and Cucumber.js
  • Leverage practical examples of unit tests covering a range of SvelteKit framework features

Description

Svelte is a popular front-end framework used for its focus on performance and user-friendliness, and test-driven development (TDD) is a powerful approach that helps in creating automated tests before writing code. By combining them, you can create efficient, maintainable code for modern applications. Svelte with Test-Driven Development will help you learn effective automated testing practices to build and maintain Svelte applications. In the first part of the book, you’ll find a guided walkthrough on building a SvelteKit application using the TDD workflow. You’ll uncover the main concepts for writing effective unit test cases and practical advice for developing solid, maintainable test suites that can speed up application development while remaining effective as the application evolves. In the next part of the book, you’ll focus on refactoring and advanced test techniques, such as using component mocks and writing BDD-style tests with the Cucumber.js framework. In the final part of the book, you’ll explore how to test complex application and framework features, including authentication, Svelte stores, and service workers. By the end of this book, you’ll be well-equipped to build test-driven Svelte applications by employing theoretical and practical knowledge.

Who is this book for?

This book is an essential guide for Svelte developers seeking to enhance their development process by learning the TDD workflow and its application. Whether you are an experienced developer or new to automated testing, this book helps you gain a practical approach to improving your workflow. The examples are written in JavaScript, making them accessible to all developers, including TypeScript developers.

What you will learn

  • Create clear and concise Vitest unit tests helping the implementation of Svelte components
  • Use Playwright and Cucumber.js to develop end-to-end tests that simulate user interactions and test the functionality of your application
  • Leverage component mocks to isolate and test individual components
  • Write unit tests for a range of Svelte framework features
  • Explore effective refactoring techniques to keep your Svelte application code and test suites clean
  • Build high-quality Svelte applications that are well-tested, performant, and resilient to changes

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jun 30, 2023
Length: 250 pages
Edition : 1st
Language : English
ISBN-13 : 9781837630950
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jun 30, 2023
Length: 250 pages
Edition : 1st
Language : English
ISBN-13 : 9781837630950
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just Mex$85 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just Mex$85 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total Mex$ 2,091.97 2,256.97 165.00 saved
Real-World Svelte
Mex$655.99 Mex$820.99
Svelte with Test-Driven Development
Mex$717.99
SvelteKit Up and Running
Mex$717.99
Total Mex$ 2,091.97 2,256.97 165.00 saved Stars icon
Banner background image

Table of Contents

21 Chapters
Part 1: Learning the TDD Cycle Chevron down icon Chevron up icon
Chapter 1: Setting up for Testing Chevron down icon Chevron up icon
Chapter 2: Introducing the Red-Green-Refactor Workflow Chevron down icon Chevron up icon
Chapter 3: Loading Data into a Route Chevron down icon Chevron up icon
Chapter 4: Saving Form Data Chevron down icon Chevron up icon
Chapter 5: Validating Form Data Chevron down icon Chevron up icon
Chapter 6: Editing Form Data Chevron down icon Chevron up icon
Part 2: Refactoring Tests and Application Code Chevron down icon Chevron up icon
Chapter 7: Tidying up Test Suites Chevron down icon Chevron up icon
Chapter 8: Creating Matchers to Simplify Tests Chevron down icon Chevron up icon
Chapter 9: Extracting Logic Out of the Framework Chevron down icon Chevron up icon
Chapter 10: Test-Driving API Endpoints Chevron down icon Chevron up icon
Chapter 11: Replacing Behavior with a Side-By-Side Implementation Chevron down icon Chevron up icon
Chapter 12: Using Component Mocks to Clarify Tests Chevron down icon Chevron up icon
Chapter 13: Adding Cucumber Tests Chevron down icon Chevron up icon
Part 3: Testing SvelteKit Features Chevron down icon Chevron up icon
Chapter 14: Testing Authentication Chevron down icon Chevron up icon
Chapter 15: Test-Driving Svelte Stores Chevron down icon Chevron up icon
Chapter 16: Test-Driving Service Workers Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.9
(10 Ratings)
5 star 90%
4 star 10%
3 star 0%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Theodore Spielman Jun 30, 2023
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I'm a few chapters into this book and wanted to put up my initial impression. I find a lot of programming books very dense and a struggle to get through, but this isn't one of them. It's surprisingly easy to read and follow along. Daniel Irvine was able to balance being informative and comprehensive enough for those new to svelte and tdd while not being verbose and tiresome. Personally, I've never been a fan of tdd, but I'm actually looking forward to diving deeper into the workflow outlined in the book and making use of it in my own projects. I'll have to follow up once I finish the book but so far packt seems to have put out another winner.
Amazon Verified review Amazon
Cameron Tully-Smith Jul 05, 2023
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Love the simplicity of the examples and the focus on TDD/overview of it. Nice approach to formalizing Svelte usage.
Amazon Verified review Amazon
Sukhmeet S Khalar Jul 03, 2023
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I'm a quarter of the way through the book, and it does a very good job of explaining Test Driven Development (TDD). It goes over the core concepts and features of TDD.Using SvelteKit is just a plus in my eyes. You don't need to know Svelte to go through this book. The author does a good job of balancing technical and practical points.
Amazon Verified review Amazon
Tim Hildegard Jul 31, 2023
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I liked the book very much. There aren't that many resources on the topic of TDD for Svelte, and the book does a nice job of showing how to use Vitest and Playwright.The book mostly uses numbered lists to show how to build up the tests and code in Red-Pass-Refactor cycles. It starts off with simple tests, and then gradually increases the complexity.The tests are all based on reasonably realistic examples, which makes the easy to port to one's own projects. On the flipside, the book can become a little repetitive, since the testing is very thorough and all code is shown. But I preferred that over having incomplete examples.
Amazon Verified review Amazon
Dylan Hildenbrand Aug 11, 2023
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Having tests to ensure code is behaving as expected is a great practice that can sometimes be tricky to implement. This book clearly demonstrates not only how to get started but how to ensure consistent practices are followed, leading to more reliable code. There are lots of different resources out there that explain testing but in my opinion, this book should be considered the definitive guide to testing SvelteKit applications.The book starts by introducing some basic testing concepts and helping the reader setup the environment to code alongside. It's highly recommended to run the code along with the book, but if you'd prefer simply to peruse it, there is a very well organized GitHub repository provided with all code samples. After introducing some basic tests, the complexity is gradually increased while the book demonstrates various testing and code refactoring techniques and practical examples. By the end of the book, I felt comfortable with testing some more advanced functionality like authentication and service workers. I can't wait to begin implementing everything taught here. I fully expect to be referring to this book very frequently throughout the refactoring process of my own applications.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.