|
| 1 | +/* |
| 2 | + * @adonisjs/assembler |
| 3 | + * |
| 4 | + * (c) AdonisJS |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +import ts from 'typescript' |
| 11 | +import { test } from '@japa/runner' |
| 12 | +import { DevServer } from '../index.js' |
| 13 | +import { setTimeout as sleep } from 'node:timers/promises' |
| 14 | + |
| 15 | +test.group('DevServer', () => { |
| 16 | + test('start() execute onDevServerStarted hook', async ({ assert, fs }) => { |
| 17 | + assert.plan(1) |
| 18 | + await fs.create('bin/server.js', `process.send({ isAdonisJS: true, environment: 'web' })`) |
| 19 | + |
| 20 | + const devServer = new DevServer(fs.baseUrl, { |
| 21 | + assets: { |
| 22 | + enabled: false, |
| 23 | + }, |
| 24 | + nodeArgs: [], |
| 25 | + scriptArgs: [], |
| 26 | + hooks: { |
| 27 | + onDevServerStarted: [ |
| 28 | + async () => ({ |
| 29 | + default: () => { |
| 30 | + assert.isTrue(true) |
| 31 | + }, |
| 32 | + }), |
| 33 | + ], |
| 34 | + }, |
| 35 | + }) |
| 36 | + |
| 37 | + await devServer.start() |
| 38 | + await sleep(600) |
| 39 | + }) |
| 40 | + |
| 41 | + test('startAndWatch() execute onDevServerStarted hook', async ({ assert, fs, cleanup }) => { |
| 42 | + assert.plan(1) |
| 43 | + await fs.create('bin/server.js', `process.send({ isAdonisJS: true, environment: 'web' })`) |
| 44 | + |
| 45 | + const devServer = new DevServer(fs.baseUrl, { |
| 46 | + assets: { |
| 47 | + enabled: false, |
| 48 | + }, |
| 49 | + nodeArgs: [], |
| 50 | + scriptArgs: [], |
| 51 | + hooks: { |
| 52 | + onDevServerStarted: [ |
| 53 | + async () => ({ |
| 54 | + default: () => { |
| 55 | + assert.isTrue(true) |
| 56 | + }, |
| 57 | + }), |
| 58 | + ], |
| 59 | + }, |
| 60 | + }) |
| 61 | + |
| 62 | + await devServer.startAndWatch(ts) |
| 63 | + cleanup(() => devServer.close()) |
| 64 | + await sleep(600) |
| 65 | + }) |
| 66 | + |
| 67 | + test('execute onSourceFileChanged hook', async ({ assert, fs, cleanup }) => { |
| 68 | + assert.plan(1) |
| 69 | + |
| 70 | + await fs.createJson('tsconfig.json', { |
| 71 | + include: ['**/*'], |
| 72 | + exclude: [], |
| 73 | + }) |
| 74 | + await fs.create('index.ts', 'console.log("hey")') |
| 75 | + await fs.create('bin/server.js', `process.send({ isAdonisJS: true, environment: 'web' })`) |
| 76 | + |
| 77 | + const devServer = new DevServer(fs.baseUrl, { |
| 78 | + assets: { |
| 79 | + enabled: false, |
| 80 | + }, |
| 81 | + nodeArgs: [], |
| 82 | + scriptArgs: [], |
| 83 | + hooks: { |
| 84 | + onSourceFileChanged: [ |
| 85 | + async () => ({ |
| 86 | + default: () => { |
| 87 | + assert.isTrue(true) |
| 88 | + }, |
| 89 | + }), |
| 90 | + ], |
| 91 | + }, |
| 92 | + }) |
| 93 | + |
| 94 | + await devServer.startAndWatch(ts) |
| 95 | + cleanup(() => devServer.close()) |
| 96 | + |
| 97 | + await sleep(100) |
| 98 | + await fs.create('index.ts', 'sdf') |
| 99 | + await sleep(10) |
| 100 | + }) |
| 101 | +}) |
0 commit comments