Skip to content

Commit

Permalink
fix: correct local invoke for non node functions
Browse files Browse the repository at this point in the history
  • Loading branch information
samchungy authored and floydspace committed Dec 4, 2021
1 parent 7c9c8f5 commit 2bfb9d0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/pre-local.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { EsbuildServerlessPlugin } from '.';

export function preLocal(this: EsbuildServerlessPlugin) {
this.serviceDirPath = this.buildDirPath;
this.serverless.config.servicePath = this.buildDirPath;
// Set service path as CWD to allow accessing bundled files correctly
process.chdir(this.serviceDirPath);
// If this is a node function set the service path as CWD to allow accessing bundled files correctly
if (this.functions[this.options.function]) {
process.chdir(this.serviceDirPath);
}
}
39 changes: 39 additions & 0 deletions src/tests/pre-local.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { preLocal } from '../pre-local';

const chdirSpy = jest.spyOn(process, 'chdir').mockImplementation();

afterEach(() => {
jest.resetAllMocks();
});

it('should call chdir with the buildDirPath if the invoked function is a node function', () => {
const esbuildPlugin = {
buildDirPath: 'workdir/.build',
serverless: {
config: {},
},
options: {
function: 'hello',
},
functions: {
hello: {},
},
};
preLocal.call(esbuildPlugin);
expect(chdirSpy).toBeCalledWith(esbuildPlugin.buildDirPath);
});

it('should not call chdir if the invoked function is not a node function', () => {
const esbuildPlugin = {
buildDirPath: 'workdir/.build',
serverless: {
config: {},
},
options: {
function: 'hello',
},
functions: {},
};
preLocal.call(esbuildPlugin);
expect(chdirSpy).not.toBeCalled();
});

0 comments on commit 2bfb9d0

Please sign in to comment.