Debugging & TestingLøsning i praktisk VS løsning i kode
"Testing er definert som en aktivitet for å sjekke om de reelle resultatene møtte de forventede resultatene og sikre at systemet er feil fri."const parseName = (firsstName, lastName) => {
return firstName + ' ' + lastName;
};
const testFuncion = () => {
const expectedNameOutput = 'Ola Nordmann';
const parsedNameValue = parseName('Ola', 'Nordmann');
if (expectedNameOutput !== parsedNameValue) {
throw(parsedNameValue + ' is not the same as ' + expectedNameOutput);
}
};package.json allerede
{
"name": "react-demo",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
//...
}
npm run test
package.json allerede
import { render, screen } from '@testing-library/react';
import App from './App';
test('renders learn react link', () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});npm run test
test() og expect() ikke er importert
create-react-app
create-react-app tilbyr globalt for alle test-filer.import { render, screen } from '@testing-library/react';
import App from './App';
test('renders learn react link', () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});describe() - 2 parametere
test() === it()
import { render, screen } from '@testing-library/react';
import App from './App';
describe('App tester', () => {
test('renders learn react link', () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
it('just a test', () => {
expect(1+1).toEqual(2);
})
}).skip() og .only()
test.only() / fit()
describe.only() / fit()
.skip() / xit()
import { render, screen } from '@testing-library/react';
import App from './App';
describe('App tester', () => {
describe('Nested App tester', () => {
//...
})
})