Skip to content

Fix #271 Skip Non-NodeJS Functions #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@
"@types/jest": "24.0.12",
"@types/lodash": "4.14.123",
"@types/node": "12.6.2",
"jest": "24.5.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it required to upgrade jest?

I feel it's a breaking change for the suite, as in current major support for Node.js v10 must be maintained and we should be able to run tests with that version

Copy link
Contributor Author

@sazzy4o sazzy4o Aug 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, seems like the latest version of jest doesn't support Node.js v10. I rolled back to old version and tested with v10. There is just a warning that typescript version is not officially supported:

ts-jest[versions] (WARN) Version 4.7.4 of typescript installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=2.7.0 <4.0.0). Please do not report issues in ts-jest if you are using unsupported versions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, in case you were wondering why typescript was updated. It does not compile with 3.9.*:

node_modules/@sinclair/typebox/typebox.d.ts:66:102 - error TS2574: A rest element type must be an array type.

66 export declare type StaticContructorParameters<T extends readonly TSchema[], P extends unknown[]> = [...{
                                                                                                        ~~~~
67     [K in keyof T]: T[K] extends TSchema ? Static<T[K], P> : never;
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68 }];
   ~

node_modules/@sinclair/typebox/typebox.d.ts:87:100 - error TS2574: A rest element type must be an array type.

87 export declare type StaticFunctionParameters<T extends readonly TSchema[], P extends unknown[]> = [...{
                                                                                                      ~~~~
88     [K in keyof T]: T[K] extends TSchema ? Static<T[K], P> : never;
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89 }];
   ~

node_modules/@sinclair/typebox/typebox.d.ts:105:21 - error TS2456: Type alias 'IntersectReduce' circularly references itself.

105 export declare type IntersectReduce<I extends unknown, T extends readonly any[]> = T extends [infer A, ...infer B] ? IntersectReduce<I & A, B> : I extends object ? I : {};
                        ~~~~~~~~~~~~~~~

node_modules/@sinclair/typebox/typebox.d.ts:105:104 - error TS2574: A rest element type must be an array type.

105 export declare type IntersectReduce<I extends unknown, T extends readonly any[]> = T extends [infer A, ...infer B] ? IntersectReduce<I & A, B> : I extends object ? I : {};
                                                                                                           ~~~~~~~~~~

node_modules/@sinclair/typebox/typebox.d.ts:105:118 - error TS2315: Type 'IntersectReduce' is not generic.

105 export declare type IntersectReduce<I extends unknown, T extends readonly any[]> = T extends [infer A, ...infer B] ? IntersectReduce<I & A, B> : I extends object ? I : {};
...

"jest": "28.1.3",
"mock-fs": "4.9.0",
"rimraf": "2.6.3",
"standard-version": "^9.3.2",
"ts-jest": "24.0.2",
"ts-jest": "28.0.7",
"tslint": "5.14.0",
"typescript": "^3.9.10"
"typescript": "^4.7.4"
},
"dependencies": {
"fs-extra": "^7.0.1",
Expand Down
2 changes: 2 additions & 0 deletions src/Serverless.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ declare namespace Serverless {
service: {
provider: {
name: string
runtime?: string
}
functions: {
[key: string]: Serverless.Function
Expand Down Expand Up @@ -38,6 +39,7 @@ declare namespace Serverless {
interface Function {
handler: string
package: Serverless.Package
runtime?: string
}

interface Layer {
Expand Down
17 changes: 14 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,25 @@ export class TypeScriptPlugin {
get functions() {
const { options } = this
const { service } = this.serverless
const functions = service.functions || {}

if (options.function) {
const nodeFunctions = Object.keys(functions)
.filter(functionKey => {
const runtime = functions[functionKey].runtime || service.provider.runtime
return runtime.includes('nodejs')
})
.reduce((obj, key) => {
obj[key] = functions[key]
return obj
}, {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not user reduce for that. It'll be clearer to setup loop as follows:

const nodeFunctions = {};
for (const [name, functionObject] of Object.entries(functions)) {
  if (nodeRuntime) nodeFunctions[name] = functionObject;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I refactored the logic


if (options.function && nodeFunctions[options.function]) {
return {
[options.function]: service.functions[this.options.function]
[options.function]: nodeFunctions[options.function]
}
}

return service.functions
return nodeFunctions
}

get rootFileNames() {
Expand Down
3 changes: 3 additions & 0 deletions tests/typescript.extractFileName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as path from 'path'
const functions: { [key: string]: Serverless.Function } = {
hello: {
handler: 'tests/assets/hello.handler',
runtime: 'nodejs10.1',
package: {
include: [],
exclude: [],
Expand All @@ -12,6 +13,7 @@ const functions: { [key: string]: Serverless.Function } = {
},
world: {
handler: 'tests/assets/world.handler',
runtime: 'nodejs10.1',
package: {
include: [],
exclude: [],
Expand All @@ -20,6 +22,7 @@ const functions: { [key: string]: Serverless.Function } = {
},
js: {
handler: 'tests/assets/jsfile.create',
runtime: 'nodejs10.1',
package: {
include: [],
exclude: [],
Expand Down
61 changes: 61 additions & 0 deletions tests/typescript.pluginSkipNonNode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as TypeScriptPlugin from '../src/index'

describe('TypeScriptPlugin', () => {
it('rootFileNames includes only node runtimes', () => {
const slsInstance: Serverless.Instance = {
cli: {
log: jest.fn()
},
config: {
servicePath: 'servicePath'
},
service: {
provider: {
name: 'aws',
runtime: 'nodejs99'
},
functions: {
func1: {
handler: 'java-fn',
runtime: 'python3.9',
package:{
exclude: [],
include: [],
patterns: []
}
},
func2: {
handler: 'node-fn',
runtime: 'nodejs16',
package:{
exclude: [],
include: [],
patterns: []
}
}
},
package: {
exclude: [],
include: [],
patterns: []
},
layers: {},
getAllLayers: jest.fn(),
getAllFunctions: jest.fn()
},
pluginManager: {
spawn: jest.fn()
}
}

const plugin = new (TypeScriptPlugin as any)(slsInstance, {})

expect(
Object.keys(plugin.functions)
).toEqual(
[
'func2'
],
)
})
})