summaryrefslogtreecommitdiff
path: root/test/helpers.js
blob: 8ca42ea116bb447a69f446916919e77fe5492bde (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { fileURLToPath } from 'url';
import { build as astroBuild } from '../lib/build.js';
import { readFile } from 'fs/promises';
import { createRuntime } from '../lib/runtime.js';
import { loadConfig } from '../lib/config.js';
import * as assert from 'uvu/assert';
/** setup fixtures for tests */
export function setup(Suite, fixturePath) {
  let runtime, setupError;

  Suite.before(async (context) => {
    const astroConfig = await loadConfig(fileURLToPath(new URL(fixturePath, import.meta.url)));

    const logging = {
      level: 'error',
      dest: process.stderr,
    };

    try {
      runtime = await createRuntime(astroConfig, { logging });
    } catch (err) {
      console.error(err);
      setupError = err;
    }

    context.runtime = runtime;
    context.readFile = async (path) => {
      const resolved = fileURLToPath(new URL(`${fixturePath}${path}`, import.meta.url));
      return readFile(resolved).then(r => r.toString('utf-8'));
    }
  });

  Suite.after(async () => {
    (await runtime) && runtime.shutdown();
  });

  Suite('No errors creating a runtime', () => {
    assert.equal(setupError, undefined);
  });
}

export function setupBuild(Suite, fixturePath) {
  let build, setupError;

  Suite.before(async (context) => {
    const astroConfig = await loadConfig(fileURLToPath(new URL(fixturePath, import.meta.url)));

    const logging = {
      level: 'error',
      dest: process.stderr,
    };

    build = (...args) => astroBuild(astroConfig, ...args);
    context.build = build;
  });

  Suite.after(async () => {
    // Shutdown i guess.
  });

  Suite('No errors creating a runtime', () => {
    assert.equal(setupError, undefined);
  });
}