archimedes-projects / archimedes-js Goto Github PK
View Code? Open in Web Editor NEWArchimedes's implementation for JavaScript and TypeScript
Home Page: https://archimedesfw.io/
License: Apache License 2.0
Archimedes's implementation for JavaScript and TypeScript
Home Page: https://archimedesfw.io/
License: Apache License 2.0
Originally posted by esuca July 27, 2021
In almost all of the projects that we use archimedes-js we also use ts-mockito for mocking commands, queries, etc.
A few months ago I discovered and used in a few projects jest-mock-extended library, which provides type-safe mocking extensions for Jest, allowing us to use the Jest APIs for all of our mocks.
jest-mock-extended is downloaded almost 160.000 times per week and has a more active development than ts-mockito.
Today I had a conversation with @cesalberca about replacing ts-mockito with jest-mock-extended in new projects and he suggested creating a new discussion here for all.
Code example:
class FirstQry {
constructor() {
}
execute(param: { foo: boolean }) {
return param.foo ? 'isFoo' : 'notFoo'
}
}
class SecondQry {
constructor(private firstQry: FirstQry) {
}
execute(foo: boolean) {
return this.firstQry.execute({ foo })
}
}
test('mocking example with ts-mockito', () => {
const firstQryMock = mock(FirstQry)
const secondQry = new SecondQry(instance(firstQryMock))
when(firstQryMock.execute(deepEqual({ foo: false }))).thenReturn('mocked')
const result = secondQry.execute(false)
expect(result).toBe('mocked')
verify(firstQryMock.execute(deepEqual({ foo: false }))).once()
})
test('1. mocking example with jest-mock-extended following its documentation example', () => {
const firstQryMock = mock<FirstQry>()
const secondQry = new SecondQry(firstQryMock)
// @ts-expect-error mockReturnValue or mockReturnValueOnce expect the return value to be 'isFoo' or 'notFoo'
// firstQryMock.execute.mockReturnValueOnce('mocked')
firstQryMock.execute.mockReturnValue('mocked')
const result = secondQry.execute(false)
expect(result).toBe('mocked')
expect(firstQryMock.execute).toHaveBeenCalledTimes(1)
expect(firstQryMock.execute).toHaveBeenCalledWith({ foo: false })
})
test('2. mocking example with jest-mock-extended using calledWith to match an object argument', () => {
const firstQryMock = mock<FirstQry>()
const secondQry = new SecondQry(firstQryMock)
// @ts-expect-error mockReturnValue expect the return value to be 'isFoo' or 'notFoo'
firstQryMock.execute.calledWith(myDeepEqual({ foo: false })).mockReturnValue('mocked')
const result = secondQry.execute(false)
expect(result).toBe('mocked')
expect(firstQryMock.execute).toHaveBeenCalledTimes(1)
expect(firstQryMock.execute).toHaveBeenCalledWith({ foo: false })
})
export const myDeepEqual: <T>(expectedValue: T) => Matcher<T> = expectedValue =>
new Matcher(actualValue => JSON.stringify(actualValue) === JSON.stringify(expectedValue))
Advantages of jest-mock-extended over ts-mockito:
Disadvantages of jest-mock-extended over ts-mockito:
These disadvantages are more limitations of the Jest API than of the library itself... And all of them have an easy workaround.
After using it in a few projects, I noticed that the tests code is cleaner and the debugging is easier because of better assertions error messages and the IDEs integration with the Jest APIs.
What do you think about using jest-mock-extended in new projects?
Part of the library: Use cases
Commands are being cached (only Queries should have this behaviour).
Sometimes we want to transform a JavaScript Date to our Datetime wrapper AND keep the local time zone.
The time zone is always UTC in the current implementation:
static fromJsDate(date: Date) {
return new Datetime(LuxonDatetime.fromJSDate(date))
}
Because the Datetime constructor already supports the isLocal argument, I think the following implementation change is the more accurate:
static fromJsDate(date: Date, isLocal: boolean = false) {
return new Datetime(LuxonDatetime.fromJSDate(date), isLocal)
}
This issue will keep track of all the work needed for the ArchimedesJS V2 release! Noteworthy changes:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.