Skip to content

Mock function supported #93

Answered by ASaiAnudeep
gpacurar53 asked this question in Q&A
Discussion options

You must be logged in to vote
Welcome to PactumJS Discussions & thanks a lot for using this library.

Pactum doesn't support mocking functions. Usually mocking independent functions happens at unit test level. If you are writing unit tests, we can use libraries like sinon or jest for mocking.

If you are writing API tests, we generally mock the external dependencies. I do understand that at some point particular dependencies cannot be mocked. As a work around, I prefer using env variables to control the behaviour. For example

function getSecret() {
  if (process.env.NODE_ENV === 'test') { return 'secret'; }

  // or else actual code will be executed to fetch the secret

}

Running the server in test environment.

export N…

Replies: 4 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by ASaiAnudeep
Comment options

You must be logged in to vote
3 replies
@ASaiAnudeep
Comment options

@martynaol
Comment options

@ASaiAnudeep
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants