GithubHelp home page GithubHelp logo

archimedes-projects / archimedes-js Goto Github PK

View Code? Open in Web Editor NEW
37.0 37.0 3.0 6.19 MB

Archimedes's implementation for JavaScript and TypeScript

Home Page: https://archimedesfw.io/

License: Apache License 2.0

JavaScript 0.86% TypeScript 96.70% CSS 2.44%
archimedes architecture ddd solid typescript

archimedes-js's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

archimedes-js's Issues

Replace ts-mockito with jest-mock-extended

Discussed in #5

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:

  1. Provides a familiar Jest like API. We don't need to learn another library.
  2. With ts-mockito we need to wrap our mock with the instance() method to make it work. Sometimes we forget to wrap it and ts-mockito does not warn us about it. With jest-mock-extended there is no need for a wrapper, we can pass directly the mock.
  3. ts-mockito verify error message does not include the actual calls with the arguments, it only provides us basic error message with the expected times call and the actually called times, making debugging harder.
  4. Supports mocking interfaces, functions, deep objects, and class instances.
  5. Provides complete Typescript type safety for interfaces, argument types, and return types.

Disadvantages of jest-mock-extended over ts-mockito:

  1. By default ts-mockito is matching the arguments by reference, and when we want to match for arguments equality, deepEqual() method is used. ts-mockito's deepEqual is not documented yet.
    Most of the time with jest-mock-extended expecting an argument matching is not necessary, because we can mock directly the function call and later assert that was called correctly as shown on the first jest-mock-extended test. But if we need it, the behaviour of jest-mock-extended is the same as ts-mockito and we could use any of the available jest matchers, like, containsValue('value') or create our own deepEqual method as I did on the second jest-mock-extended test.
  2. jest-mock-extended uses the Jest assertion API and there is no toHaveBeenCalledWithOnce() assertion method that combines the
    toHaveBeenCalledTimes(n) and toHaveBeenCalledWith(value) into one single assertion... There is something similar, but not as semantic: expect(firstQryMock.execute).toHaveBeenNthCalledWith(1, { foo: false }). We could wrap it with our own assertion to give it a better naming.

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?

Commands are being cached

Part of the library: Use cases

Issue

Commands are being cached (only Queries should have this behaviour).

Add isLocal argument to fromJsDate method of Datetime

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)
  }

v2 release! ๐Ÿš€

This issue will keep track of all the work needed for the ArchimedesJS V2 release! Noteworthy changes:

  • Runner is async now
  • HttpClient now returns an HttpClientResponse object
  • New Archimedes class used for entrypoint to all Archimedes configuration like the runner
  • Add UseCaseKey decorator
  • Improve cache handling

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.