Closed lukevd closed 1 year ago
Is this package intended to be used for testing an angular application
Not specifically, but yes it's quite possible to test Angular applications with Mockttp.
Error: src/app/app.component.spec.ts:3:20 - error TS2591: Cannot find name 'require'. Do you need to install type definitions for node? Try
npm i --save-dev @types/node
and then add 'node' to the types field in your tsconfig.
This message is because you're using ESM modules (import ... from ...
) but you've used require
in your test file. This isn't really a Mockttp issue - it applies to using any JS library.
You shouldn't need to import the node types for that specifically (I'm not sure if they're required for other reasons) instead you should import all libraries using ESM syntax instead of require, e.g:
import * as superagent from 'superagent';
import * as mockttp from 'mockttp';
const mockServer = mockttp.getLocal();
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default...
This is because you're using Webpack v5 - as noted in each of those messages, this means you need to explicitly include libraries for features that older webpack (and most other bundlers) would normally include automatically.
Mockttp's own browser test suite is tested against Webpack v5, and you can find the config here which may be useful. I think the important part is the resolve.fallback
section, which in Mockttp's tests looks like:
assert: require.resolve('assert/'),
buffer: require.resolve('buffer/'),
crypto: require.resolve('crypto-browserify'),
zlib: require.resolve('browserify-zlib'),
stream: require.resolve('stream-browserify'),
path: require.resolve('path-browserify'),
querystring: require.resolve('querystring-es3'),
util: require.resolve('util/'),
url: require.resolve('url/')
You may not need all of those, in Mockttp's case there's quite a few additional modules that are used only because they're helpful in the tests, not within Mockttp itself.
You'll also need to install each of the modules listed from npm, if you don't have them already.
mockServer.start(8080)
This line in your beforeEach
should have an await
, to ensure the tests don't start before the server is running.
It would be useful to remove some of these node dependencies in future, although using them does make life much easier for non-browser use cases, and most bundlers (except Webpack v5+) do handle this transparently. I'm also open to documenting all this more clearly. In both cases, PRs are very welcome!
Thank's. I decided to not integrate mockttp directly in my angular tests but to use it in a pure node.js application.
Is this package intended to be used for testing an angular application (https://angular.io/guide/testing)? If yes, then the documentation should be improved in my oppinion.
I created an angular application by typing in the following command
(as described here: https://angular.io/guide/setup-local). Then, i typed in the following commands as described in the getting started section in the readme of mockttp:
I created the test as described in the section Get Testing in the readme of mockttp:
Running the tests by executing the command
ng test
produced the following error:Therefore, i installed node (
npm i --save-dev @types/node
) and added node to the filetsconfig.spec.json
(which is not documented in the readme of mockttp):I also installed superagent:
Running the tests by executing the command
ng test
produces the following output: