implement in project
vue+ts+jest
https://cli.vuejs.org/config/#babel . ctrl+f5 搜尋jest,並搭配下面這個
https://cli.vuejs.org/migrating-from-v3/#vue-cli-plugin-unit-jest
What's the difference between faking, mocking, and stubbing?https://stackoverflow.com/questions/346372/whats-the-difference-between-faking-mocking-and-stubbing?rq=1
https://ithelp.ithome.com.tw/articles/10187403 Iron man
// functions.test.js or functions.spec.js
const functions = import(./functions)
//beforeAll(), afterAll()
beforeEach(()=>initDatabase());
afterEach(()=>closeDatabase());
const initDatabase = () =>console.log('Database Initialized...');
const closeDatabase = () =>console.log('Database Closed...');
// describe限制beforeEach,...的作用域
describe('描述這個作用域中的動作目的', () =>{
beforeEach(() => initDatabase());
test('anything to describe this test', () => {
const user ="Jeff";
expect(user).not.toBe("David")
})
})
test('anything to describe this test', () => { // test也可以寫成it
expect(functions.add(2,2)).toBe(4)
})
test('anything to describe this test', () => {
expect(functions.add(2,2)).not.toBe(5)
})
// toBeNull()
// toBeUndefined()
// toBeDefined() is the opposite of toBeUndefined()
// toBeTruthy() matches anything that an if statement treats as true
// toBeFalsy() matches anything that an if statement treats as false
test('anything to describe this test', () => {
expect(functions.isNull()).toBeNull()
})
// object or array use toEqual() not toBe(), because they are reference types not primitive types, but toEqual() also can use on primitive types
test('anything to describe this test', () => {
expect(functions.createUser()).toEqual({
firstName:'Ni',
lastName:'diiff'
})
})
// toBeLessThanOrEqual()
// toBeGreaterThan()
test('anything to describe this test', () => {
const a=800;
const b=700;
expect(a+b).toBeLessThan(1600)
})
// Regex
test('anything to describe this test', () => {
expect('team').toMatch(/I/)
})
// Arrays
test('anything to describe this test', () => {
usernames = ['join', 'same', 'admin']
expect(usernames).toContain('admin')
})
// Promise
test('anything to describe this test', () => {
expect.assertions(1); // 斷言接下來有幾個異步執行
return functions.fetchUser().then(data => { // 就是個api的動作
expect(data.name).toEqual('Leanne Graham')
})
})
//Async Await (等於promise換個寫法!)
test('anything to describe this test', async () => {
expect.assertions(1);
const data = await functions.fetchUser();
expect(data.name).toEqual('David')
})
沒有留言:
張貼留言