|
| 1 | +import type { LoaderThis } from '../../src/config/loaders/types'; |
| 2 | +import type { ValueInjectionLoaderOptions } from '../../src/config/loaders/valueInjectionLoader'; |
| 3 | +import valueInjectionLoader from '../../src/config/loaders/valueInjectionLoader'; |
| 4 | + |
| 5 | +const defaultLoaderThis = { |
| 6 | + addDependency: () => undefined, |
| 7 | + async: () => undefined, |
| 8 | + cacheable: () => undefined, |
| 9 | + callback: () => undefined, |
| 10 | +}; |
| 11 | + |
| 12 | +const loaderThis = { |
| 13 | + ...defaultLoaderThis, |
| 14 | + resourcePath: './client.config.ts', |
| 15 | + getOptions() { |
| 16 | + return { |
| 17 | + values: { |
| 18 | + foo: 'bar', |
| 19 | + }, |
| 20 | + }; |
| 21 | + }, |
| 22 | +} satisfies LoaderThis<ValueInjectionLoaderOptions>; |
| 23 | + |
| 24 | +describe('valueInjectionLoader', () => { |
| 25 | + it('should correctly insert values for basic config', () => { |
| 26 | + const userCode = ` |
| 27 | + import * as Sentry from '@sentry/nextjs'; |
| 28 | + Sentry.init(); |
| 29 | + `; |
| 30 | + |
| 31 | + const result = valueInjectionLoader.call(loaderThis, userCode); |
| 32 | + |
| 33 | + expect(result).toMatchSnapshot(); |
| 34 | + expect(result).toMatch(';globalThis["foo"] = "bar";'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should correctly insert values with directive', () => { |
| 38 | + const userCode = ` |
| 39 | + "use client" |
| 40 | + import * as Sentry from '@sentry/nextjs'; |
| 41 | + Sentry.init(); |
| 42 | + `; |
| 43 | + |
| 44 | + const result = valueInjectionLoader.call(loaderThis, userCode); |
| 45 | + |
| 46 | + expect(result).toMatchSnapshot(); |
| 47 | + expect(result).toMatch(';globalThis["foo"] = "bar";'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should correctly insert values with directive and semicolon', () => { |
| 51 | + const userCode = ` |
| 52 | + "use client"; |
| 53 | + import * as Sentry from '@sentry/nextjs'; |
| 54 | + Sentry.init(); |
| 55 | + `; |
| 56 | + |
| 57 | + const result = valueInjectionLoader.call(loaderThis, userCode); |
| 58 | + |
| 59 | + expect(result).toMatchSnapshot(); |
| 60 | + expect(result).toMatch(';globalThis["foo"] = "bar";'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should correctly insert values with directive and inline comments', () => { |
| 64 | + const userCode = ` |
| 65 | + // test |
| 66 | + "use client"; |
| 67 | + import * as Sentry from '@sentry/nextjs'; |
| 68 | + Sentry.init(); |
| 69 | + `; |
| 70 | + |
| 71 | + const result = valueInjectionLoader.call(loaderThis, userCode); |
| 72 | + |
| 73 | + expect(result).toMatchSnapshot(); |
| 74 | + expect(result).toMatch(';globalThis["foo"] = "bar";'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should correctly insert values with directive and block comments', () => { |
| 78 | + const userCode = ` |
| 79 | + /* test */ |
| 80 | + "use client"; |
| 81 | + import * as Sentry from '@sentry/nextjs'; |
| 82 | + Sentry.init(); |
| 83 | + `; |
| 84 | + |
| 85 | + const result = valueInjectionLoader.call(loaderThis, userCode); |
| 86 | + |
| 87 | + expect(result).toMatchSnapshot(); |
| 88 | + expect(result).toMatch(';globalThis["foo"] = "bar";'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should correctly insert values with directive and multiline block comments', () => { |
| 92 | + const userCode = ` |
| 93 | + /* |
| 94 | + test |
| 95 | + */ |
| 96 | + "use client"; |
| 97 | + import * as Sentry from '@sentry/nextjs'; |
| 98 | + Sentry.init(); |
| 99 | + `; |
| 100 | + |
| 101 | + const result = valueInjectionLoader.call(loaderThis, userCode); |
| 102 | + |
| 103 | + expect(result).toMatchSnapshot(); |
| 104 | + expect(result).toMatch(';globalThis["foo"] = "bar";'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should correctly insert values with directive and multiline block comments and a bunch of whitespace', () => { |
| 108 | + const userCode = ` |
| 109 | + /* |
| 110 | + test |
| 111 | + */ |
| 112 | +
|
| 113 | +
|
| 114 | +
|
| 115 | +
|
| 116 | + "use client"; |
| 117 | +
|
| 118 | +
|
| 119 | +
|
| 120 | + import * as Sentry from '@sentry/nextjs'; |
| 121 | + Sentry.init(); |
| 122 | + `; |
| 123 | + |
| 124 | + const result = valueInjectionLoader.call(loaderThis, userCode); |
| 125 | + |
| 126 | + expect(result).toMatchSnapshot(); |
| 127 | + expect(result).toMatch(';globalThis["foo"] = "bar";'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should correctly insert values with a misplaced directive', () => { |
| 131 | + const userCode = ` |
| 132 | + console.log('This will render the directive useless'); |
| 133 | + "use client"; |
| 134 | +
|
| 135 | +
|
| 136 | +
|
| 137 | + import * as Sentry from '@sentry/nextjs'; |
| 138 | + Sentry.init(); |
| 139 | + `; |
| 140 | + |
| 141 | + const result = valueInjectionLoader.call(loaderThis, userCode); |
| 142 | + |
| 143 | + expect(result).toMatchSnapshot(); |
| 144 | + expect(result).toMatch(';globalThis["foo"] = "bar";'); |
| 145 | + }); |
| 146 | +}); |
0 commit comments