The COVID-19 pandemic drove a big sales surge for Costco Wholesale (NASDAQ:COST) once the initial round of lockdowns eased last spring. Costco's sales momentum continued into 2021, thanks to a broader surge in retail spending.

That said, June represented a test of sorts for the warehouse club giant, as it faced its toughest year-over-year comparison yet. Unsurprisingly, Costco passed the test with flying colors, delivering another strong sales increase last month.

Sales trends remain strong

In May, Costco logged a 14.7% adjusted comparable sales gain, excluding the impact of gasoline price and exchange rate fluctuations. Management noted that the timing of Memorial Day boosted comp sales growth by at least 2 percentage points. Nevertheless, the results were quite good given that adjusted comparable sales had increased 9.7% in the prior-year period.

Costco faced an even tougher year-over-year comparison in June: Adjusted comp sales surged 14.4% in the year-ago period. And while the timing of Memorial Day boosted growth in May, it reduced Costco's comps by 1.5 to 2 percentage points last month, according to the company.

Nevertheless, Costco posted a solid 7.9% adjusted comp sales increase for the June retail month, with high single-digit growth in each of its major regions. The numbers came in solidly ahead of analysts' expectations. With the pandemic easing in most of the retailer's major markets, traffic is bouncing back, rising 8.1% in comparable stores last month.

The entrance to a Costco warehouse.

Image source: Costco Wholesale.

On an unadjusted basis, Costco recorded an even stronger 14.1% comp sales gain last month, as gasoline prices have surged over the past year and the U.S. dollar has weakened against foreign currencies. Meanwhile, total sales jumped 16.9% to $18.92 billion.

Maintaining gains in food, while discretionary sales soar

In the early months of the pandemic, food and other consumer staples drove a disproportionate amount of Costco's growth, as people cooked at home more often and tried to consolidate shopping trips.

Despite the resulting tough year-over-year comparisons, Costco posted low single-digit comp sales growth in food and sundries and mid-single-digit comp sales growth in fresh foods last month. Costco's ability to continue growing sales in these categories bodes well for the sustainability of its 2020 market-share gains. That's a testament to the loyalty of its membership base.

On the flip side, ancillary sales plunged last year, due to low demand for gasoline and travel, as well as temporary closures or reduced services in Costco's food courts, optical shops, and hearing aid departments. Ancillary sales have bounced back nicely in 2021, including growth exceeding 40% in June. Costco's gasoline volume surpassed 2019 levels last month. Its food courts and optical departments also posted particularly strong gains.

Lastly, demand for discretionary items like toys, jewelry, and sporting goods remains red hot. Comp sales for Costco's nonfood merchandise grew at a double-digit pace last month, on top of double-digit growth a year earlier.

Well-deserved gains for Costco stock

Costco shares slumped earlier this year as investors rotated away from 2020's winners in favor of beaten-down stocks that were expected to benefit from economic reopening. However, Costco stock has come roaring back over the past four months, jumping nearly 30% to reach new all-time highs.

COST Chart

Costco Wholesale stock performance, data by YCharts.

The warehouse club giant deserves these recent gains. While year-over-year comparisons will become a bit tougher over the next few months, Costco's strong June sales performance should give investors confidence that comps will remain positive for the foreseeable future. That should solidify the earnings tailwinds from higher store productivity.

Strong sales will also indirectly drive growth in membership fee income by justifying future increases to Costco's membership fees and encouraging more people to upgrade to pricier "executive" memberships.

Costco stock may look expensive at nearly 36 times forward earnings. However, between the company's strong growth, its substantial long-term expansion potential, and its massive moat, Costco clearly deserves its premium valuation.

This article represents the opinion of the writer, who may disagree with the “official” recommendation position of a Motley Fool premium advisory service. We’re motley! Questioning an investing thesis -- even one of our own -- helps us all think critically about investing and make decisions that help us become smarter, happier, and richer.