At no point did the USSR ally with the Nazis, because fascism and communism are enemies at the most fundamental level. Fascism is the capitalist class it its most ruthless, while communism is anti-capitalist to its core.
If the USSR was allied with the Nazis, what does that say about the states that signed non-aggression pacts before it did? For many of them, they were treaties of desperation, not allyship.

The USSR only signed that after its offer of a million Soviet troops to help fight the Nazis was rejected by the UK and France. It only signed it after all other options had been exhausted. They signed it in the hopes of stalling Germany’s inevitable invasion, giving them time to build up their military strength. And it worked.
Germany invaded Poland, and the Polish government collapsed. In response, the USSR went into Poland to keep Germany from occupying all of Poland, which would have broken the agreement regarding “spheres of influence.” And in fact Germany did break that agreement. Once Germany entered Poland, all of Poland would have been occupied by the Nazis if the USSR had done nothing, and that would have put the German forces right on the USSR’s border.