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

Unit tests #19

Merged
merged 5 commits into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
6 changes: 6 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
Copy link
Owner

Choose a reason for hiding this comment

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

why do we need both the file .babelrc and babel.config.js? Can we delete .babelrc?

Copy link
Author

Choose a reason for hiding this comment

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

yes, I think so. I will delete it.

"presets": [
["@babel/preset-env", {"targets": {"node": "current"}}],
'@babel/preset-typescript',
],
}
6 changes: 6 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
Copy link
Owner

Choose a reason for hiding this comment

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

I need to rename babel.config.js to babel.config.cjs to make it work. Otherwise I get error error while loading config - You appear to be using a native ECMAScript module configuration file, which is only supported when running Babel asynchronously.

Copy link
Author

Choose a reason for hiding this comment

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

this works for me either

presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript',
],
};
5 changes: 5 additions & 0 deletions jest-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
presets: [
'@testing-library/jest-dom/extend-expect',
],
};
16,585 changes: 11,605 additions & 4,980 deletions package-lock.json

Large diffs are not rendered by default.

25 changes: 24 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"dev": "rollup -c -w",
"start": "sirv public",
"validate": "svelte-check",
"release": "dotenv release-it"
"release": "dotenv release-it",
"test": "jest src",
"test:watch": "npm run test -- --watch"
},
"release-it": {
"hooks": {
Expand All @@ -31,16 +33,21 @@
"author": "Malte Bonart",
"license": "MIT",
"devDependencies": {
"@babel/preset-typescript": "^7.16.7",
"@fortawesome/free-regular-svg-icons": "^5.15.3",
"@rollup/plugin-commonjs": "^19.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^11.0.0",
"@rollup/plugin-typescript": "^6.0.0",
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/svelte": "^3.0.3",
"@tsconfig/svelte": "^1.0.10",
"@types/marked": "^2.0.3",
"acorn": "^8.2.4",
"babel-jest": "^27.4.6",
"css-tree": "^1.1.3",
"dotenv-cli": "^4.0.0",
"jest": "^27.4.7",
"postcss": "^8.3.5",
"release-it": "^14.6.1",
"rollup": "^2.39.0",
Expand All @@ -53,15 +60,18 @@
"sirv-cli": "^1.0.0",
"svelte": "^3.43.2",
"svelte-check": "^1.0.0",
"svelte-jester": "^2.1.5",
"svelte-preprocess": "^4.0.0",
"tslib": "^2.0.0",
"typescript": "^3.9.3"
},
"repository": "https://github.com/bonartm/quizdown-js",
"main": "public/build/quizdown.js",
"dependencies": {
"@babel/preset-env": "^7.16.8",
"@fortawesome/fontawesome-svg-core": "^1.2.35",
"@fortawesome/free-solid-svg-icons": "^5.15.3",
"@types/jest": "^27.4.0",
"auto-bind": "^4.0.0",
"dompurify": "^2.2.6",
"highlight.js": "^10.7.0",
Expand All @@ -70,5 +80,18 @@
"strip-indent": "^3.0.0",
"svelte-i18n": "^3.3.9",
"yaml": "2.0.0-5"
},
"jest": {
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.svelte$": "svelte-jester"
},
"moduleFileExtensions": [
"js",
"svelte"
],
"setupFilesAfterEnv": [
"./jest-setup.ts"
]
}
}
32 changes: 32 additions & 0 deletions src/components/Button.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @jest-environment jsdom
*/

import '@testing-library/jest-dom'

import {render, fireEvent} from '@testing-library/svelte'

import Button from "./Button.svelte";
describe("Button component", () => {
window.alert = jest.fn();
window.alert.mockClear();

test("should confirm there is a button in the Button component ", () => {
const { container } = render(Button);
expect(container).toContainHTML("<button");
expect(container).toContainHTML("</button>");
});

test('show alert when the button gets clicked', async () => {
const { getByTestId, findByRole } = render(Button, {title: 'testButton'});
Copy link
Owner

@bonartm bonartm Feb 6, 2022

Choose a reason for hiding this comment

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

In the spirit of the guiding principles, it is recommended to use this only after the other queries don't work for your use case. Using data-testid attributes do not resemble how your software is used and should be avoided if possible. (https://testing-library.com/docs/queries/bytestid)

I would recommend to use getByRole as the button element has the default role button

const button = getByTestId('testButton');

// Using await when firing events is unique to the svelte testing library because
// we have to wait for the next `tick` so that Svelte flushes all pending state changes.
await fireEvent.click(button)

const alertBox = findByRole('alert');

expect(alertBox).toBeInTheDocument;
});
});
2 changes: 1 addition & 1 deletion src/components/Button.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
export let title = '';
</script>

<button title="{title}" disabled="{disabled}" on:click="{buttonAction}">
<button data-testid="testButton" title="{title}" disabled="{disabled}" on:click="{buttonAction}">
Copy link
Owner

Choose a reason for hiding this comment

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

When we use getByRole we also don't need to introduce data-testid

<slot />
</button>

Expand Down
11 changes: 9 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
"types": ["marked", "svelte"]
},

"include": ["src/**/*"],
"exclude": ["node_modules/*", "__sapper__/*", "public/*", "docs/*"]
"include": [
"src/**/*",
"./jest-setup.module.ts"
],
"exclude": [
"node_modules/*",
"__sapper__/*",
"public/*", "docs/*"
]
}