@labdigital/commercetools-mock 2.5.0 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,9 +3,16 @@
3
3
  [<img src="https://img.shields.io/npm/v/@labdigital/commercetools-mock">](https://www.npmjs.com/package/@labdigital/commercetools-mock)
4
4
  [![codecov](https://codecov.io/gh/labd/commercetools-node-mock/branch/main/graph/badge.svg?token=muKkNunJ95)](https://codecov.io/gh/labd/commercetools-node-mock)
5
5
 
6
- This library mocks the Commercetools rest api to ease testing of your typescript
7
- codebases interacting with the commercetools api. It uses the same proven approach
8
- as our testing module in the [commercetools Python SDK](https://github.com/labd/commercetools-python-sdk/tree/main/src/commercetools/testing).
6
+ This library mocks the Commercetools rest API to ease testing of your typescript
7
+ codebases interacting with the commercetools api. It uses the same proven
8
+ approach as our testing module in the
9
+ [commercetools Python SDK](https://github.com/labd/commercetools-python-sdk/tree/main/src/commercetools/testing).
10
+
11
+ Since version 2 of this library it is based on [msw](https://mswjs.io/) instead
12
+ of nock. It is now therefore als recommended to manage the msw server yourself
13
+ and use the `registerHandlers` method to register the handlers on this server.
14
+
15
+ This allows you to use the same server for mocking other API's as well.
9
16
 
10
17
  ## Installation
11
18
 
@@ -15,13 +22,15 @@ yarn add --dev @labdigital/commercetools-mock
15
22
 
16
23
  ## Docker image
17
24
 
18
- This codebase is also available as a docker image where it provides a runnable http server exposing the mocked endpoints. See
19
- https://github.com/labd/commercetools-mock-server
25
+ This codebase is also available as a docker image where it provides a runnable
26
+ http server exposing the mocked endpoints. See
27
+ https://hub.docker.com/r/labdigital/commercetools-mock-server
20
28
 
21
29
  ## Example
22
30
 
23
31
  ```typescript
24
32
  import { CommercetoolsMock, getBaseResourceProperties } from '@labdigital/commercetools-mock'
33
+ import { setupServer } from 'msw/node'
25
34
 
26
35
  const ctMock = new CommercetoolsMock({
27
36
  apiHost: 'https://localhost',
@@ -34,7 +43,8 @@ const ctMock = new CommercetoolsMock({
34
43
 
35
44
  describe('A module', () => {
36
45
  beforeAll(() => {
37
- ctMock.start()
46
+ const server = setupServer()
47
+ ctMock.registerHandlers(server)
38
48
 
39
49
  ctMock.project().add('type', {
40
50
  ...getBaseResourceProperties()
@@ -44,6 +54,7 @@ describe('A module', () => {
44
54
  })
45
55
 
46
56
  afterAll(() => {
57
+ server.clearHandlers()
47
58
  ctMock.stop()
48
59
  })
49
60
 
@@ -61,7 +72,9 @@ describe('A module', () => {
61
72
  })
62
73
  ```
63
74
 
64
- ## Adding a resource
75
+ ## Contributing
76
+
77
+ ### Adding a new service
65
78
 
66
79
  Implement the following:
67
80
 
@@ -71,7 +84,7 @@ Implement the following:
71
84
  - Add new service to src/storage.ts InMemoryStorage
72
85
  - Adjust src/types.ts RepositoryMap and possibly serviceTypes
73
86
 
74
- ## Releasing
87
+ ### Releasing
75
88
 
76
89
  This codebases use [@changesets](https://github.com/changesets/changesets) for release and version management
77
90
 
package/dist/index.cjs CHANGED
@@ -6992,22 +6992,17 @@ var CommercetoolsMock = class {
6992
6992
  });
6993
6993
  return app;
6994
6994
  }
6995
- startServer() {
6996
- if (_globalListeners.length > 0) {
6997
- if (this._mswServer !== void 0) {
6998
- throw new Error("Server already started");
6999
- } else {
7000
- console.warn("Server wasn't stopped properly, clearing");
7001
- _globalListeners.forEach((listener) => listener.close());
7002
- }
7003
- }
7004
- const server = this.app;
7005
- this._mswServer = (0, import_node.setupServer)(
6995
+ // registerHandlers is an alternative way to work with commercetools-mock, it
6996
+ // allows you to manage msw server yourself and register the handlers needed
6997
+ // for commercetools-mock to work.
6998
+ registerHandlers(server) {
6999
+ const app = this.app;
7000
+ server.use(
7006
7001
  import_msw.http.post(`${this.options.authHost}/oauth/*`, async ({ request }) => {
7007
7002
  const body = await request.text();
7008
7003
  const url = new URL(request.url);
7009
7004
  const headers = copyHeaders(request.headers);
7010
- const res = await (0, import_light_my_request.default)(server).post(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
7005
+ const res = await (0, import_light_my_request.default)(app).post(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
7011
7006
  return new import_msw.HttpResponse(res.body, {
7012
7007
  status: res.statusCode,
7013
7008
  headers: mapHeaderType(res.headers)
@@ -7017,7 +7012,7 @@ var CommercetoolsMock = class {
7017
7012
  const body = await request.text();
7018
7013
  const url = new URL(request.url);
7019
7014
  const headers = copyHeaders(request.headers);
7020
- const res = await (0, import_light_my_request.default)(server).get(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
7015
+ const res = await (0, import_light_my_request.default)(app).get(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
7021
7016
  if (res.statusCode === 200) {
7022
7017
  const parsedBody = JSON.parse(res.body);
7023
7018
  const resultCount = "count" in parsedBody ? parsedBody.count : Object.keys(parsedBody).length;
@@ -7035,7 +7030,7 @@ var CommercetoolsMock = class {
7035
7030
  const body = await request.text();
7036
7031
  const url = new URL(request.url);
7037
7032
  const headers = copyHeaders(request.headers);
7038
- const res = await (0, import_light_my_request.default)(server).get(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
7033
+ const res = await (0, import_light_my_request.default)(app).get(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
7039
7034
  return new import_msw.HttpResponse(res.body, {
7040
7035
  status: res.statusCode,
7041
7036
  headers: mapHeaderType(res.headers)
@@ -7045,7 +7040,7 @@ var CommercetoolsMock = class {
7045
7040
  const body = await request.text();
7046
7041
  const url = new URL(request.url);
7047
7042
  const headers = copyHeaders(request.headers);
7048
- const res = await (0, import_light_my_request.default)(server).post(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
7043
+ const res = await (0, import_light_my_request.default)(app).post(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
7049
7044
  return new import_msw.HttpResponse(res.body, {
7050
7045
  status: res.statusCode,
7051
7046
  headers: mapHeaderType(res.headers)
@@ -7055,14 +7050,26 @@ var CommercetoolsMock = class {
7055
7050
  const body = await request.text();
7056
7051
  const url = new URL(request.url);
7057
7052
  const headers = copyHeaders(request.headers);
7058
- const res = await (0, import_light_my_request.default)(server).delete(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
7053
+ const res = await (0, import_light_my_request.default)(app).delete(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
7059
7054
  return new import_msw.HttpResponse(res.body, {
7060
7055
  status: res.statusCode,
7061
7056
  headers: mapHeaderType(res.headers)
7062
7057
  });
7063
7058
  })
7064
7059
  );
7065
- this._mswServer.listen({
7060
+ }
7061
+ startServer() {
7062
+ if (_globalListeners.length > 0) {
7063
+ if (this._mswServer !== void 0) {
7064
+ throw new Error("Server already started");
7065
+ } else {
7066
+ console.warn("Server wasn't stopped properly, clearing");
7067
+ _globalListeners.forEach((listener) => listener.close());
7068
+ }
7069
+ }
7070
+ const server = (0, import_node.setupServer)();
7071
+ this.registerHandlers(server);
7072
+ server.listen({
7066
7073
  // We need to allow requests done by supertest
7067
7074
  onUnhandledRequest: (request, print) => {
7068
7075
  const url = new URL(request.url);
@@ -7072,7 +7079,8 @@ var CommercetoolsMock = class {
7072
7079
  print.error();
7073
7080
  }
7074
7081
  });
7075
- _globalListeners.push(this._mswServer);
7082
+ _globalListeners.push(server);
7083
+ this._mswServer = server;
7076
7084
  }
7077
7085
  };
7078
7086
  // Annotate the CommonJS export names for ESM import in node: