File size: 2,260 Bytes
23ac194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
'use strict'

const { test } = require('node:test')
const fp = require('../plugin')

test('webpack removes require.main.filename', t => {
  const filename = require.main.filename
  const info = console.info
  t.after(() => {
    require.main.filename = filename
    console.info = info
  })

  require.main.filename = null

  console.info = function (msg) {
    t.assert.fail('logged: ' + msg)
  }

  fp((fastify, opts, next) => {
    next()
  }, {
    fastify: '^5.0.0'
  })
})

test('support faux modules', (t) => {
  const plugin = fp((fastify, opts, next) => {
    next()
  })

  t.assert.strictEqual(plugin.default, plugin)
})

test('support faux modules does not override existing default field in babel module', (t) => {
  const module = {
    default: (fastify, opts, next) => next()
  }

  module.default.default = 'Existing default field'

  const plugin = fp(module)

  t.assert.strictEqual(plugin.default, 'Existing default field')
})

test('support ts named imports', (t) => {
  const plugin = fp((fastify, opts, next) => {
    next()
  }, {
    name: 'hello'
  })

  t.assert.strictEqual(plugin.hello, plugin)
})

test('from kebab-case to camelCase', (t) => {
  const plugin = fp((fastify, opts, next) => {
    next()
  }, {
    name: 'hello-world'
  })

  t.assert.strictEqual(plugin.helloWorld, plugin)
})

test('from @-prefixed named imports', (t) => {
  const plugin = fp((fastify, opts, next) => {
    next()
  }, {
    name: '@hello/world'
  })

  t.assert.strictEqual(plugin.helloWorld, plugin)
})

test('from @-prefixed named kebab-case to camelCase', (t) => {
  const plugin = fp((fastify, opts, next) => {
    next()
  }, {
    name: '@hello/my-world'
  })

  t.assert.strictEqual(plugin.helloMyWorld, plugin)
})

test('from kebab-case to camelCase multiple words', (t) => {
  const plugin = fp((fastify, opts, next) => {
    next()
  }, {
    name: 'hello-long-world'
  })

  t.assert.strictEqual(plugin.helloLongWorld, plugin)
})

test('from kebab-case to camelCase multiple words does not override', (t) => {
  const fn = (fastify, opts, next) => {
    next()
  }

  const foobar = {}
  fn.helloLongWorld = foobar

  const plugin = fp(fn, {
    name: 'hello-long-world'
  })

  t.assert.strictEqual(plugin.helloLongWorld, foobar)
})