Explorar o código

test(core): Create e2e test for ProcessContext

Michael Bromley %!s(int64=5) %!d(string=hai) anos
pai
achega
d318caf5f3

+ 42 - 1
packages/core/e2e/fixtures/test-plugins.ts

@@ -1,5 +1,6 @@
 import { Controller, Get, Injectable } from '@nestjs/common';
 import { Query, Resolver } from '@nestjs/graphql';
+import { MessagePattern } from '@nestjs/microservices';
 import { Permission } from '@vendure/common/lib/generated-shop-types';
 import { LanguageCode } from '@vendure/common/lib/generated-types';
 import {
@@ -7,13 +8,19 @@ import {
     ConfigModule,
     ConfigService,
     InternalServerError,
+    Logger,
     OnVendureBootstrap,
     OnVendureClose,
     OnVendureWorkerBootstrap,
     OnVendureWorkerClose,
+    PluginCommonModule,
+    ProcessContext,
     VendurePlugin,
+    WorkerMessage,
+    WorkerService,
 } from '@vendure/core';
 import gql from 'graphql-tag';
+import { of } from 'rxjs';
 
 export class TestPluginWithAllLifecycleHooks
     implements OnVendureBootstrap, OnVendureWorkerBootstrap, OnVendureClose, OnVendureWorkerClose {
@@ -174,7 +181,7 @@ export class TestPluginWithProvider {}
 
 @VendurePlugin({
     imports: [ConfigModule],
-    configuration: config => {
+    configuration: (config) => {
         // tslint:disable-next-line:no-non-null-assertion
         config.defaultLanguageCode = LanguageCode.zh;
         return config;
@@ -220,3 +227,37 @@ export class TestController {
     controllers: [TestController],
 })
 export class TestRestPlugin {}
+
+class TestWorkerMessage extends WorkerMessage<string, boolean> {
+    static readonly pattern = 'TestWorkerMessage';
+}
+
+@Controller('process-context')
+export class TestProcessContextController {
+    constructor(private processContext: ProcessContext, private workerService: WorkerService) {}
+
+    @Get('server')
+    isServer() {
+        return this.processContext.isServer;
+    }
+
+    @Get('worker')
+    isWorker() {
+        return this.workerService.send(new TestWorkerMessage('hello'));
+    }
+}
+@Controller()
+export class TestProcessContextWorkerController {
+    constructor(private processContext: ProcessContext) {}
+
+    @MessagePattern(TestWorkerMessage.pattern)
+    isWorker() {
+        return of(this.processContext.isWorker);
+    }
+}
+@VendurePlugin({
+    imports: [PluginCommonModule],
+    controllers: [TestProcessContextController],
+    workers: [TestProcessContextWorkerController],
+})
+export class TestProcessContextPlugin {}

+ 21 - 0
packages/core/e2e/plugin.e2e-spec.ts

@@ -13,6 +13,7 @@ import {
     TestPluginWithAllLifecycleHooks,
     TestPluginWithConfigAndBootstrap,
     TestPluginWithProvider,
+    TestProcessContextPlugin,
     TestRestPlugin,
 } from './fixtures/test-plugins';
 
@@ -39,6 +40,7 @@ describe('Plugins', () => {
             TestPluginWithProvider,
             TestLazyExtensionPlugin,
             TestRestPlugin,
+            TestProcessContextPlugin,
         ],
     });
 
@@ -161,6 +163,25 @@ describe('Plugins', () => {
         });
     });
 
+    describe('processContext', () => {
+        it('server context', async () => {
+            const response = await shopClient.fetch(
+                `http://localhost:${testConfig.port}/process-context/server`,
+            );
+            const body = await response.text();
+
+            expect(body).toBe('true');
+        });
+        it('worker context', async () => {
+            const response = await shopClient.fetch(
+                `http://localhost:${testConfig.port}/process-context/worker`,
+            );
+            const body = await response.text();
+
+            expect(body).toBe('true');
+        });
+    });
+
     describe('on app close', () => {
         beforeAll(async () => {
             await server.destroy();