Live share
dmtrKovalenko
@dmtrKovalenko
Unit testing
Integration testing
e2e testing
e2e
Integration
Unit
e2e
Integration
Unit
непонятно
Rest API
e2e
Front-end
Cypress.Commands.add('login', () => {
cy.visit('/login')
cy.get('#email').type('admin@cypress.io')
cy.get('#password').type('security')
cy.get('#submit-btn').click()
})
cy.login()
Cypress.Commands.add('login', () => {
cy.request('/testRoutes/loginAsAdmin')
})
e2e
Integration
Unit
Мы тут
React
Angular 1.0
Vue
JQuery
2006
2012
2013
2014
2019
Testing Pyramid
2008
https://benhutchison.wordpress.com/2008/03/20/automated-testing-2-the-test-pyramid/
(запускать и писать)
(писать и поддерживать)
не e2e?
Мы тут
React
Angular 1.0
Vue
JQuery
2006
2012
2013
2014
2019
Testing 🔼
2008
2004
Selenium
Build Time: 42:14
Мы тут
React
Angular 1.0
Vue
JQuery
2006
2012
2013
2014
2019
Testing 🔼
2008
2004
Selenium
version: 2
jobs:
build:
docker:
- image: cypress/base:10
steps:
- checkout
- restore_cache:
name: Restore Yarn Package Cache
keys:
- yarn-packages-{{ checksum "yarn.lock" }}
- run:
name: Install Dependencies
command: yarn install --frozen-lockfile
- save_cache:
name: Save Yarn Package Cache
key: yarn-packages-{{ checksum "yarn.lock" }}
paths:
- ~/.cache
- run: cypress run --record
version: 2
jobs:
build:
docker:
- image: cypress/base:10
parallelism: 15
steps:
- checkout
- restore_cache:
name: Restore Yarn Package Cache
keys:
- yarn-packages-{{ checksum "yarn.lock" }}
- run:
name: Install Dependencies
command: yarn install --frozen-lockfile
- save_cache:
name: Save Yarn Package Cache
key: yarn-packages-{{ checksum "yarn.lock" }}
paths:
- ~/.cache
- run: cypress run --record
e2e
Integration
import db from './db'
const router = express.Router();
router.get('/users', (req, res) => {
db.getAllUsers().then(res.json)
})
const router = express.Router();
router.get('/users', (req, res) => {
req.db.getAllUsers().then(res.json)
})
Rest API
Frontend
GraphQL
import { fetchFromApi } from '~/fetcher'
function fetchUsers() {
return fetchFromApi('/users')
}
it('Should fetch users', async () => {
jest.mock(fetchFromApi, () => Promise.resolve([{ id: 1 }]))
const result = await fetchUsers()
expect(result).toEqual([{ id: 1 }])
})
/root/div/descendant::*[not (@class='bad')] //h1/following-sibling::ul[1]
<div data-test-id="user-list-name"/>
e2e
Integration
Unit
Надежность
Скорость
it("renders three <Foo /> components", () => {
const wrapper = shallow(
<MyComponent>
<Foo />
<Foo />
<Foo />
</MyComponent>
);
expect(wrapper.find(Foo)).to.have.lengthOf(3);
});
const Component = () => (
<MyComponent>
<Foo />
<Foo />
<Foo />
</MyComponent>
)
const jsx = React.createElement
const Component = () => jsx(
MyComponent,
null,
jsx(Foo, null),
jsx(Foo, null),
jsx(Foo, null)
);
it("renders three <Foo /> components", () => {
const wrapper = shallow(jsx(
MyComponent,
null,
jsx(Foo, null),
jsx(Foo, null),
jsx(Foo, null)
));
const arguments = jsx.mock.calls[0]
expect(arguments.filter((element) => element.type === Foo).to.have.lengthOf(3)
});
Text
vs
export function calculateUsagePeriods(
currentUsagePeriodStartsAt,
currentUsagePeriodEndsAt,
organizationCreatedAt,
n = 6
) {
if (!currentUsagePeriodStartsAt) return []
const usagePeriods = []
let startTime = moment.utc(currentUsagePeriodStartsAt)
let endTime = moment.utc(currentUsagePeriodEndsAt)
for (let i = 0; i < n; i++) {
if (endTime.isBefore(organizationCreatedAt)) {
break
}
usagePeriods.push({
startTime: startTime.clone(),
endTime: endTime.clone(),
})
startTime.subtract(1, 'month')
endTime.subtract(1, 'month')
}
return usagePeriods
}
test("Should calculate usage periods when n = 5", () => {
const result = calculateUsagePeriods(new Date(), addDays(new Date(), 5), 5)
expect(result).toEqual([...])
})
test.each`
startsAt | endsAt | n | expected
${new Date()} | ${new Date()} | ${10} | ${[15, 16]}
${new Date()} | ${addDays(new Date(), 10)} | ${6} | ${[15, 16]}
${new Date()} | ${addDays(new Date(), -10)} | ${15} | ${[15, 16]}
`(
"Should calculate usage periods when startsAt = $startsAt, endsAt = $endsAt n = $n",
({ startsAt, endsAt, n, expected }) => {
expect(calculateUsagePeriods(startsAt, endsAt, n)).toEqual(expected);
}
);
e2e
Integration
Unit