![]() ![]() ![]() This makes sense for screenshots (since screenshots taken on different platforms will deviate slightly, for example because of text rendering), but it rarely makes sense in case of textual/binary data.In a series of posts which will span the research of myself and my colleague Herb Williams, we will talk about our conclusions and general messing about with tools to test WebSockets. Playwright will append a platform-specific suffix (like linux) to the name of the file the snapshot is stored in. Playwright Test has a method expect(.).toMatchSnapshot(.) which can perform snapshot-comparisons on screenshots but also textual and binary data. This is the biggest downside of using Playwright for unit tests in my opinion.įor now, if you want to test modules isolated from others you probably have to adapt the codebase to use some kind of dependency injection.īe it that dependencies are just passed as arguments when calling functions, or using a dependency injection solution like NestJS IoC containers (which I have used very successfully in the past). If you want to get module mocking support in Playwright, you can upvote this feature request: /microsoft/playwright/issues/14572. Although there are some Node.js libraries that allow module mocking (like testdouble), they don't work because Playwright does some of the module loading itself ( /microsoft/playwright/issues/14398). Unfortunately, this is not possible in Playwright. Module mocking allows to replace the "thing" returned by require/ import statements by some fake instance. Some test frameworks provide a way to mock CommonJS/ECMAScript modules. Set environment variable PWTEST_WATCH=1 when running Playwright. There is experimental support for Watch mode at the time of writing, see /microsoft/playwright/issues/21960#issuecomment-1483604692. Take a look at the example repository I made to see how to set up those things!įor Mocks & Spies use sinon or jest-mock (the mocking solution shipped with Jest).įor Fake timers use sinon ( See code changes).Ĭode coverage just needs a simple combination of the command-line interface nyc of Istanbul (a code coverage tool for JavaScript) and source-map-support ( See code changes). Most of those things can be substituted by using libraries of the Node.js ecosystem. Things missing from Playwright (and how to fill the gaps) #Ĭompared to Jest, there are some things missing from Playwright Test which are often needed when running unit tests. ![]() Newer test runners like vitest also do not set globals anymore. Having the same import statements from in every test file might be some boilerplate, but I think the reduced friction (by not having any globals) justifies that boilerplate. There are a couple of workarounds to avoid those compilation errors, for example fiddling with (multiple) tsconfig.json files such that types of these libraries are excluded.Ĭypress even has a guide dedicated to this problem ( /guides/tooling/typescript-support#Clashing-types-with-Jest). In TypeScript projects, as soon as you install any two of these test libraries you end up with TypeScript compilation errors, since the types of those functions are usually not compatible with each other. Those test runners set up their test functions as globals ( it, expect, describe, beforeEach etc.). This is in my opinion much better than the beforeEach / afterEach fiddling you have to do with other test runners.Ĭopy // base.ts import ) Ĭompare that to existing test runners like Jest and Mocha (and Cypress, using Mocha under-the-hood). "fixtures" allow to prepare resources per test case on an "opt-in" basis. Playwright has some very useful features, e.g.: Playwright Test supports TypeScript out-of-the-box.įor all other test runners I am aware of you have to configure some transform (Babel, ts-jest, etc.), or run the test runner with ts-node, or compile the test cases before running them (see e.g. One tool less in your setup means one tool less to learn, configure, update, etc. I think there are some advantages of using Playwright for unit tests: I had used "traditional" test runners before, mainly Jest, but also Mocha a few years ago. Why to use Playwright Test for unit tests # suggest some minor tweaks to make Playwright more suitable for unit tests.outline things missing from Playwright compared to other test frameworks (and how to fill some of those gaps).describe what I like about Playwright and why I think it can be a good choice for unit testing.This means the test subject is executed directly in Node.js this differs from the "usual" way of using Playwright to open a browser, navigate to a web page, and test that web page in the browser. Recently, I started using Playwright Test, the test runner of Playwright ( for unit tests also. I have used Playwright for a couple of my projects so far, and I had such a good experience that it got my E2E test automation tool of choice. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |