decorator-dependency-injection 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,124 @@
1
+ name: Create Release
2
+
3
+ on:
4
+ push:
5
+ branches: [ "main" ]
6
+ pull_request:
7
+ branches: [ "main" ]
8
+
9
+ jobs:
10
+ build:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ matrix:
14
+ node-version: [22.x, 23.x]
15
+ # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
16
+ steps:
17
+ - uses: actions/checkout@v4
18
+ - name: Use Node.js ${{ matrix.node-version }}
19
+ uses: actions/setup-node@v4
20
+ with:
21
+ node-version: ${{ matrix.node-version }}
22
+ cache: 'npm'
23
+ - run: npm ci
24
+ - run: npm run build --if-present
25
+ - run: npm test
26
+
27
+ release:
28
+ needs: build
29
+ runs-on: ubuntu-latest
30
+ permissions:
31
+ contents: write
32
+ steps:
33
+ - name: Checkout Repository
34
+ uses: actions/checkout@v4
35
+
36
+ - name: Install Node.js
37
+ uses: actions/setup-node@v4
38
+ with:
39
+ node-version: '22'
40
+
41
+ - name: Get package.json version
42
+ id: package_version
43
+ run: echo "VERSION=$(node -p "require('./package.json').version")" >> $GITHUB_ENV
44
+
45
+ - name: Check if tag exists
46
+ id: check_tag
47
+ run: |
48
+ TAG_EXISTS=$(git ls-remote --tags origin "v${VERSION}" | wc -l)
49
+ echo "TAG_EXISTS=${TAG_EXISTS}" >> $GITHUB_ENV
50
+
51
+ - name: Create Tag and Release
52
+ uses: actions/github-script@v7
53
+ with:
54
+ script: |
55
+ const version = process.env.VERSION
56
+ const tag = `v${version}`
57
+ const { data: tags } = await github.rest.repos.listTags({
58
+ owner: context.repo.owner,
59
+ repo: context.repo.repo,
60
+ })
61
+
62
+ if (!tags.find(t => t.name === tag)) {
63
+ await github.rest.git.createRef({
64
+ owner: context.repo.owner,
65
+ repo: context.repo.repo,
66
+ ref: `refs/tags/${tag}`,
67
+ sha: context.sha,
68
+ })
69
+
70
+ await github.rest.repos.createRelease({
71
+ owner: context.repo.owner,
72
+ repo: context.repo.repo,
73
+ tag_name: tag,
74
+ name: `Release ${tag}`,
75
+ body: `Automated release of version ${tag}`,
76
+ draft: false,
77
+ prerelease: false,
78
+ })
79
+ core.notice(`Release ${tag} created.`)
80
+ } else {
81
+ core.notice(`Release ${tag} already exists. No new release created.`)
82
+ }
83
+
84
+ publish:
85
+ needs: release
86
+ runs-on: ubuntu-latest
87
+ steps:
88
+ - name: Checkout Repository
89
+ uses: actions/checkout@v4
90
+
91
+ - name: Install Node.js
92
+ uses: actions/setup-node@v4
93
+ with:
94
+ node-version: '22'
95
+ registry-url: 'https://registry.npmjs.org/'
96
+
97
+ - name: Get package.json version
98
+ id: package_version
99
+ run: echo "VERSION=$(node -p "require('./package.json').version")" >> $GITHUB_ENV
100
+
101
+ - name: Check if version exists on npm
102
+ id: check_npm_version
103
+ run: |
104
+ if npm show ${{ github.repository }}@${{ env.VERSION }} > /dev/null 2>&1; then
105
+ echo "VERSION_EXISTS=true" >> $GITHUB_ENV
106
+ else
107
+ echo "VERSION_EXISTS=false" >> $GITHUB_ENV
108
+ fi
109
+
110
+ - name: Publish to npm
111
+ if: env.VERSION_EXISTS == 'false'
112
+ run: npm publish --access public
113
+ env:
114
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
115
+
116
+ - name: Notice Publish Status
117
+ if: env.VERSION_EXISTS == 'false'
118
+ run: echo "::notice::Published new version ${{ env.VERSION }} to npm."
119
+
120
+ - name: Notice Publish Status
121
+ if: env.VERSION_EXISTS == 'true'
122
+ run: echo "::notice::Version ${{ env.VERSION }} already exists on npm. No new version published."
123
+
124
+
package/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  ## Description
7
7
 
8
- With TC39 reaching stage 3 on the decorators proposal, it's time to start thinking about how we can use them in our projects. One of the most common patterns in JavaScript is dependency injection. This pattern is used to make our code more testable and maintainable. This library provides simple decorators to help you inject dependencies into your classes and mock them for testing.
8
+ With [TC39](https://github.com/tc39/proposal-decorators) reaching stage 3 on the decorators proposal, it's time to start thinking about how we can use them in our projects. One of the most common patterns in JavaScript is dependency injection. This pattern is used to make our code more testable and maintainable. This library provides simple decorators to help you inject dependencies into your classes and mock them for testing.
9
9
 
10
10
  ## Installation
11
11
 
package/index.js CHANGED
@@ -118,10 +118,16 @@ export function Mock(mockedClazzOrName) {
118
118
  }
119
119
  if (singletons.has(mockedClazzOrName)) {
120
120
  const instanceContext = singletons.get(mockedClazzOrName)
121
+ if (instanceContext.original) {
122
+ throw new Error('Mock already defined, reset before mocking again')
123
+ }
121
124
  instanceContext.original = instanceContext.clazz
122
125
  instanceContext.clazz = clazz
123
126
  } else if (factories.has(mockedClazzOrName)) {
124
127
  const instanceContext = factories.get(mockedClazzOrName)
128
+ if (instanceContext.original) {
129
+ throw new Error('Mock already defined, reset before mocking again')
130
+ }
125
131
  instanceContext.original = instanceContext.clazz
126
132
  instanceContext.clazz = clazz
127
133
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "decorator-dependency-injection",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "A simple library for dependency injection using decorators",
5
5
  "author": "Ravi Gairola <mallox@pyxzl.net>",
6
6
  "license": "Apache-2.0",
package/test/mock.test.js CHANGED
@@ -12,6 +12,10 @@ describe('Mocking', () => {
12
12
  @Inject(ToBeMockedSingleton) toBeMockedSingleton
13
13
  }
14
14
 
15
+ afterEach(() => {
16
+ resetMocks()
17
+ })
18
+
15
19
  it('should inject a mock singleton', () => {
16
20
  @Mock(ToBeMockedSingleton)
17
21
  class MockedSingleton {
@@ -54,4 +58,14 @@ describe('Mocking', () => {
54
58
  const result2 = new TestInjectionFactory()
55
59
  expect(result2.toBeMockedFactory.op()).toBe('original')
56
60
  })
61
+
62
+ it('should throw an error if a mock is not a singleton or factory', () => {
63
+ expect(() => {
64
+ @Mock(ToBeMockedFactory)
65
+ class Mocked1 {}
66
+
67
+ @Mock(ToBeMockedFactory)
68
+ class Mocked2 {}
69
+ }).toThrow('Mock already defined, reset before mocking again')
70
+ })
57
71
  })
@@ -1,31 +0,0 @@
1
- # This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2
- # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
3
-
4
- name: Node.js CI
5
-
6
- on:
7
- push:
8
- branches: [ "main" ]
9
- pull_request:
10
- branches: [ "main" ]
11
-
12
- jobs:
13
- build:
14
-
15
- runs-on: ubuntu-latest
16
-
17
- strategy:
18
- matrix:
19
- node-version: [18.x, 20.x, 22.x]
20
- # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
21
-
22
- steps:
23
- - uses: actions/checkout@v4
24
- - name: Use Node.js ${{ matrix.node-version }}
25
- uses: actions/setup-node@v4
26
- with:
27
- node-version: ${{ matrix.node-version }}
28
- cache: 'npm'
29
- - run: npm ci
30
- - run: npm run build --if-present
31
- - run: npm test
@@ -1,13 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="WEB_MODULE" version="4">
3
- <component name="NewModuleRootManager">
4
- <content url="file://$MODULE_DIR$">
5
- <excludeFolder url="file://$MODULE_DIR$/.tmp" />
6
- <excludeFolder url="file://$MODULE_DIR$/temp" />
7
- <excludeFolder url="file://$MODULE_DIR$/tmp" />
8
- </content>
9
- <orderEntry type="inheritedJdk" />
10
- <orderEntry type="sourceFolder" forTests="false" />
11
- <orderEntry type="library" name="@types/jest" level="application" />
12
- </component>
13
- </module>
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="GitToolBoxBlameSettings">
4
- <option name="version" value="2" />
5
- </component>
6
- </project>
@@ -1,15 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="GitToolBoxProjectSettings">
4
- <option name="commitMessageIssueKeyValidationOverride">
5
- <BoolValueOverride>
6
- <option name="enabled" value="true" />
7
- </BoolValueOverride>
8
- </option>
9
- <option name="commitMessageValidationEnabledOverride">
10
- <BoolValueOverride>
11
- <option name="enabled" value="true" />
12
- </BoolValueOverride>
13
- </option>
14
- </component>
15
- </project>
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="JavaScriptLibraryMappings">
4
- <file url="file://$PROJECT_DIR$" libraries="{Node.js Core, decorator-depdenency-injection/node_modules}" />
5
- <file url="file://$PROJECT_DIR$/test" libraries="{@types/jest, Node.js Core, decorator-depdenency-injection/node_modules}" />
6
- <excludedPredefinedLibrary name="HTML" />
7
- </component>
8
- </project>
@@ -1,13 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="MaterialThemeProjectNewConfig">
4
- <option name="metadata">
5
- <MTProjectMetadataState>
6
- <option name="migrated" value="true" />
7
- <option name="pristineConfig" value="false" />
8
- <option name="userId" value="-1911ddbb:1739357c210:-8000" />
9
- <option name="version" value="8.13.2" />
10
- </MTProjectMetadataState>
11
- </option>
12
- </component>
13
- </project>
package/.idea/modules.xml DELETED
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/decorator-depdenency-injection.iml" filepath="$PROJECT_DIR$/.idea/decorator-depdenency-injection.iml" />
6
- </modules>
7
- </component>
8
- </project>
package/.idea/vcs.xml DELETED
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="VcsDirectoryMappings">
4
- <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
- </component>
6
- </project>